您是否需要膨胀视图才能获取 onClick 方法的 id?
您是否需要膨胀视图才能获取 onClick 方法的 id?因为当我在模拟器中运行程序并单击特定按钮时,它什么也没做!我希望它回到 main.xml 布局!我需要以其他方式处理 onClick 吗?
public void onClick(View v) {
switch( v.getId()){
case R.id.play:
setContentView(R.layout.play);
setQuestion();
break;
case R.id.level:
setContentView(R.layout.level);
if(lvl.equals("1")) {
lvl1.setChecked(true);
}
if(lvl.equals("2")) {
lvl2.setChecked(true);
}
if(lvl.equals("3")) {
lvl3.setChecked(true);
}
if(lvl.equals("4")) {
lvl4.setChecked(true);
}
if(lvl.equals("5")) {
lvl5.setChecked(true);
}
break;
case R.id.setLevel:
if(lvl1.isChecked()) {
setLevel("1");
}
if(lvl2.isChecked()) {
setLevel("2");
}
if(lvl3.isChecked()) {
setLevel("3");
}
if(lvl4.isChecked()) {
setLevel("4");
}
if(lvl5.isChecked()) {
setLevel("5");
}
setContentView(R.layout.main);
break;
}
}
以下是我获取视图的方式:
setContentView(R.layout.main);
Button play = (Button)findViewById(R.id.play);
play.setOnClickListener(this);
Button level = (Button)findViewById(R.id.level);
level.setOnClickListener(this);
Button setLevel = (Button)getLayoutInflater().inflate(R.layout.level, null).findViewById(R.id.setLevel);
setLevel.setOnClickListener(this);
lvl1 = (RadioButton)getLayoutInflater().inflate(R.layout.level, null).findViewById(R.id.lvl1);
lvl2 = (RadioButton)getLayoutInflater().inflate(R.layout.level, null).findViewById(R.id.lvl2);
lvl3 = (RadioButton)getLayoutInflater().inflate(R.layout.level, null).findViewById(R.id.lvl3);
lvl4 = (RadioButton)getLayoutInflater().inflate(R.layout.level, null).findViewById(R.id.lvl4);
lvl5 = (RadioButton)getLayoutInflater().inflate(R.layout.level, null).findViewById(R.id.lvl5);
我应该做什么,以便当我单击 setLevel 按钮时,它将视图更改回 main.xml 视图
Do you need to inflate a view in order to get its id for the onClick method? Because when I run my program in the emulator and click on the specific button it does nothing! I want it to go back to the main.xml layout! Do I need to procces the onClick some other way?
public void onClick(View v) {
switch( v.getId()){
case R.id.play:
setContentView(R.layout.play);
setQuestion();
break;
case R.id.level:
setContentView(R.layout.level);
if(lvl.equals("1")) {
lvl1.setChecked(true);
}
if(lvl.equals("2")) {
lvl2.setChecked(true);
}
if(lvl.equals("3")) {
lvl3.setChecked(true);
}
if(lvl.equals("4")) {
lvl4.setChecked(true);
}
if(lvl.equals("5")) {
lvl5.setChecked(true);
}
break;
case R.id.setLevel:
if(lvl1.isChecked()) {
setLevel("1");
}
if(lvl2.isChecked()) {
setLevel("2");
}
if(lvl3.isChecked()) {
setLevel("3");
}
if(lvl4.isChecked()) {
setLevel("4");
}
if(lvl5.isChecked()) {
setLevel("5");
}
setContentView(R.layout.main);
break;
}
}
Here is how I get the views:
setContentView(R.layout.main);
Button play = (Button)findViewById(R.id.play);
play.setOnClickListener(this);
Button level = (Button)findViewById(R.id.level);
level.setOnClickListener(this);
Button setLevel = (Button)getLayoutInflater().inflate(R.layout.level, null).findViewById(R.id.setLevel);
setLevel.setOnClickListener(this);
lvl1 = (RadioButton)getLayoutInflater().inflate(R.layout.level, null).findViewById(R.id.lvl1);
lvl2 = (RadioButton)getLayoutInflater().inflate(R.layout.level, null).findViewById(R.id.lvl2);
lvl3 = (RadioButton)getLayoutInflater().inflate(R.layout.level, null).findViewById(R.id.lvl3);
lvl4 = (RadioButton)getLayoutInflater().inflate(R.layout.level, null).findViewById(R.id.lvl4);
lvl5 = (RadioButton)getLayoutInflater().inflate(R.layout.level, null).findViewById(R.id.lvl5);
What should I do so that when I click the setLevel button it changes the view back to the main.xml view
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Andrew,
您可能想为每个项目创建单独的
onClick()
方法。这就是我处理可点击对象的方式。示例:
这样您就已经根据其 ID 值创建了按钮对象,并且 onClickListener 是专门针对该项目定制的。
祝你好运!
Andrew,
You may want to make individual
onClick()
methods for each item. That's how I handle my click-able objects.Example:
This way you already have the button object created based on its ID value, and the onClickListener is specifically tailored to that item.
good luck!