对于我的两个按钮,我想实现一个开关案例,点击后我得到以下错误如何解决?
void是一个无效的类型 变量onClick 令牌上的语法错误“)”,;预期
这是我的代码。
Button regmalebtn = (Button) findViewById(R.id.regmalebtn);
Button regfemalebtn = (Button) findViewById(R.id.regfemalebtn);
String gender;
gender = regmalebtn.getText().toString();
public void onClick(View v) {
switch(v.getId()){
case R.id.regmalebtn:
// String gender;
// gender = regmalebtn.getText().toString();
gender.equals("M");
// request.addProperty("gender",gender );
break;
case R.id.regfemalebtn:
gender = regfemalebtn.getText().toString();
gender.equals("F");
//request.addProperty("gender", gender);
break;
default:
break;
}
}
答案 0 :(得分:8)
您收到以下错误
令牌上的语法错误“)”,;预期
That means onClick() method is defined inside another method. It should be defined outside of any method in the same class
由于 迪帕克
答案 1 :(得分:1)
请删除 默认: 打破;
来自案件R.id.regfemalebtn:
代码必须如下所示:
public void onClick(View v) {
switch(v.getId()){
case R.id.regmalebtn:
// String gender;
// gender = regmalebtn.getText().toString();
gender.equals("M");
// request.addProperty("gender",gender );
break;
case R.id.regfemalebtn:
gender = regfemalebtn.getText().toString();
gender.equals("F");
//request.addProperty("gender", gender);
break;
}
default:
break;
}
希望它可以帮到你
答案 2 :(得分:1)
public class Sample extends Activity implements OnClickListener
{
public void onCreate(Bundle bundle)
{
super.onCreate(bundle);
setContentView(//ur layout);
Button regmalebtn = (Button) findViewById(R.id.regmalebtn);
Button regfemalebtn = (Button) findViewById(R.id.regfemalebtn);
regmalebtn.setOnClickListener(this);
regfemalebtn.setOnClickListener(this);
String gender;
gender = regmalebtn.getText().toString();
}//onCreate()
public void onClick(View v) {
switch(v.getId()){
case R.id.regmalebtn:
//String gender;
// gender = regmalebtn.getText().toString();
gender.equals("M");
// request.addProperty("gender",gender );
break;
case R.id.regfemalebtn:
gender = regfemalebtn.getText().toString();
gender.equals("F");
//request.addProperty("gender", gender);
break;
default:
break;
}//onclick()
}//Sample
试试这段代码..