Android 无法将按钮名称解析为变量
我回答的最后一个问题确实帮助了我。谢谢你这么说。 但现在我又犯了一个新错误。这是我的代码:Android 无法将按钮名称解析为变量,android,Android,我回答的最后一个问题确实帮助了我。谢谢你这么说。 但现在我又犯了一个新错误。这是我的代码: import android.app.Activity; import android.os.Bundle; import android.app.AlertDialog; import android.view.View; public class Trial extends Activity { /** Called when the activity is first created. *
import android.app.Activity;
import android.os.Bundle;
import android.app.AlertDialog;
import android.view.View;
public class Trial extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View b1 = findViewById(R.id.button1);
b1.setOnClickListener(yourListener);
View b2 = findViewById(R.id.button2);
b1.setOnClickListener(yourListener);
}
View.OnClickListener yourListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
if (v == button1) {
new AlertDialog.Builder(v.getContext())
.setTitle("Paracettamol")
.setMessage(
"This medicine is generally used to cure Fever")
.setNeutralButton("OK", null).show();
} else if (v == button2) {
new AlertDialog.Builder(v.getContext())
.setTitle("sertraline")
.setMessage(
"This medicine is generally used to cure Head aches")
.setNeutralButton("OK", null).show();
}
}
};
}
首先,我想告诉你们两件事:
1) 在最后一行中,我经常得到一个错误:“Syntax error,insert”;“完成现场申报。
2) 我插入“;,并保存了它,然后我得到一个if错误,否则if行分别是“button1不能解析为变量”和“button2不能解析为变量”
我的main.xml代码如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:id="@+id/button1"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="@string/s1"
android:onClick="b1"/>
<Button android:id="@+id/button2"
android:layout_height="wrap_content"
android:text="Belladona"
android:layout_width="fill_parent"
android:onClick="b2"/>
</LinearLayout>
任何人都可以帮助我。提前谢谢。好吧,按钮1和按钮2不存在。您需要在每个按钮前面放置
R.id.
。只需使用以下条件:
if (v.getId() == R.id.button1) { /* ... */ }
如果有更多控件,您甚至可以对视图ID使用
switch
。这是您的java文件
import android.app.Activity;
import android.os.Bundle;
import android.app.AlertDialog;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Trial extends Activity implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b1 = findViewById(R.id.button1);
Button b2 = findViewById(R.id.button2);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int id = v.getId();
if (id == R.id.button1) {
new AlertDialog.Builder(v.getContext())
.setTitle("Paracettamol")
.setMessage("This medicine is generally used to cure Fever")
.setNeutralButton("OK", null).show();
} else if (id == R.id.button2) {
new AlertDialog.Builder(v.getContext())
.setTitle("sertraline")
.setMessage(
"This medicine is generally used to cure Head aches")
.setNeutralButton("OK", null).show();
}
}
}
这是您的XML文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:id="@+id/button1"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="@string/s1"
/>
<Button android:id="@+id/button2"
android:layout_height="wrap_content"
android:text="Belladona"
android:layout_width="fill_parent"
/>
</LinearLayout>
我已经编辑了你的两个文件。。
希望它能对您有所帮助。您可以像这样使用
equals
方法,而不是编写总是返回false的v==button1
,因为它们都是不同的对象
if(v.equals(b1))
{
...................
}
else if(v.equals(b2))
{
................
}
你可以试试这个:
public class Trial extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b1 = (Button)findViewById(R.id.button1);
Button b2 = (Button)findViewById(R.id.button2);t
b1.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
if(b1.getText().equals("button1")) //No need to check this condition
{
//Your logic
}
}
});
b2.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
if(b2.getText().equals("button2")) //No need to check this condition
{
//Your logic
}
}
});
}尝试使用此代码解决您的问题
public class ButtonCheck extends Activity {
/** Called when the activity is first created. */
View b1,b2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b1 = (View)findViewById(R.id.button1);
b1.setOnClickListener(yourListener);
b2 =(View) findViewById(R.id.button2);
b2.setOnClickListener(yourListener);
}
View.OnClickListener yourListener=new OnClickListener() {
@Override
public void onClick(View v) {
if (v ==b1) {
new AlertDialog.Builder(v.getContext())
.setTitle("Paracettamol")
.setMessage(
"This medicine is generally used to cure Fever")
.setNeutralButton("OK", null).show();
} else if (v ==b2) {
new AlertDialog.Builder(v.getContext())
.setTitle("sertraline")
.setMessage(
"This medicine is generally used to cure Head aches")
.setNeutralButton("OK", null).show();
}
}
};
}
您好,如果您的问题得到解决,请检查并投票表决。哦,这是一件很简单的事情。谢谢you@TejasTamkhane只要点击这个答案的右边,你就可以看到向上的箭头,所以就向上。好的