gpt4 book ai didi

java - 摆脱android中的 "=="语法错误

转载 作者:行者123 更新时间:2023-11-29 10:06:43 25 4
gpt4 key购买 nike

elseif(v == button2) 行给出了一个错误,指出“ token '==' 上的语法错误,删除此 token ”。我从这个网站的主题“Variable OnClick listener android”中得到了使用它的想法。谁能告诉我如何使用它?

这是我的代码:

    View.OnClickListener yourListener = new View.OnClickListener(){
public void onClick(View v){
if( v == button1){
new AlertDialog.Builder(this)
.setTitle("Paracettamol")
.setMessage("This medicine is generally used to cure Fever")
.setNeutralButton("OK", null)
.show();}
}
elseif( v == button2){
new AlertDialog.Builder(this)

.setTitle("sertraline")
.setMessage("This medicine is generally used to cure Head aches")
.setNeutralButton("OK", null)
.show();

}



@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub

}
} ;
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub

}
}

上面提到的问题的答案有如下代码:

Button btn1, btn2;
public void onCreate(Bundle b)
{
// here you do normal things like assigning a
// content view to the activity, initiate buttons, etc.

// then you assign the same listener to both buttons
btn1.setOnClickListener(yourListener);
btn2.setOnClickListener(yourListener);
}

// declare a OnClickListener that will execute different actions
// depending on the view that was clicked
View.OnClickListener yourListener = new View.OnClickListener(){
public void onClick (View v){
if( v == btn1 ){
// do something
}
elseif( v == btn1 ){
// do another thing
}
}
};

最佳答案

你可能错过了 else 和 if 之间的空格 - "elseif( v == button2) "

关于java - 摆脱android中的 "=="语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6251965/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com