可以嵌套OnClickListener,即。
setContentView(R.layout.activity_main);
add = (Button) findViewById(R.id.bAdd);
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
setContentView(R.layout.secondary);
sub = (Button) findViewById(R.id.bSub);
}
有效吗?假设在单击时,activity_main.xml中有一个名为Sub的按钮应该转到另一个视图?
答案 0 :(得分:0)
是的你可以,但这不是好的做法......
为什么你不能使用两个Activities
并使用Intents
来切换它们..:)
示例代码..
public class MainActivity extends Activity {
Button buttonone;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonone = (Button) findViewById(R.id.buttonone);
buttonone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,
SecondaryActivity.class);
startActivity(intent);
}
});
}
}
public class SecondaryActivity extends Activity {
Button buttontwo;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondary);
buttontwo = (Button) findViewById(R.id.buttontwo);
buttontwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//随便......
}
});
}
}