我回答的最后一个问题确实对我有帮助。谢谢你们。 但现在我收到了一个新的错误。这是我的代码:
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)在最后一行,我曾经得到一个错误:“语法错误,插入”;“完成FieldDeclaration。 2)我插入“;”,并保存它,然后我得到一个错误的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>
任何人都可以帮助我。提前致谢。
答案 0 :(得分:2)
好吧,button1和button2不存在。您需要在R.id.
之前加上{{1}}。
答案 1 :(得分:0)
只需使用此条件:
if (v.getId() == R.id.button1) { /* ... */ }
如果您有更多控件,您甚至可以将switch
用于视图ID。
答案 2 :(得分:0)
这是 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>
我已经编辑了你的两个文件.. 希望它会对你有所帮助。
答案 3 :(得分:0)
您可以使用v == button1
这样的方法
equals
,因为它们都是不同的对象。
if(v.equals(b1))
{
...................
}
else if(v.equals(b2))
{
................
}
答案 4 :(得分:0)
你可以尝试这个:
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
}
}
});
}
答案 5 :(得分:0)
尝试使用此代码来解决您的问题
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();
}
}
};
}
如果你得到解决就问好,请检查并投票。