0

你能嵌套一个 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 的按钮应该转到另一个视图?

4

1 回答 1

0

是的,您可以,但这不是一个好习惯...为什么不能使用 TwoActivities和 UseIntents来切换它们..:)

示例代码..

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) {

// 做任何事... } });

}

}

于 2012-08-09T10:58:46.547 回答