我想在android中使用多个按钮打开多个布局

时间:2016-03-22 14:16:40

标签: android

我可以点击1按钮打开1个新活动(布局),但是, 我想打开4个不同的活动(布局),主页上有4个不同的按钮点击。 我尝试过切换但失败了。

3 个答案:

答案 0 :(得分:1)

试试这个:

在按钮声明中:

from sys import argv

script, slave = argv
print "Superme master, slave %s is waiting for your order." % slave

slave_says = "Humble %s wants know your supreme name, master:\n" % slave
name = raw_input(slave_says)

want = raw_input("What do you want, my superme master?\n")
if want == "Make you freedom.":
    print "When you make me freedom. you are also make yourself freedom."
else:
    print "Yes, my superme master."

和听众......

 btn1= (Button) findViewById(R.id.btn1);
 btn1.setOnClickListener(click);

 btn2= (Button) findViewById(R.id.btn2);
 btn2.setOnClickListener(click);

 btn3= (Button) findViewById(R.id.btn3);
 btn3.setOnClickListener(click);

 btn4= (Button) findViewById(R.id.btn4);
 btn4.setOnClickListener(click);

答案 1 :(得分:0)

按钮点击监听器..

but1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        open(v);
    }
});

对所有按钮重复此操作

创建方法以打开其他活动

public void open(View v){
    Intent intent = null; 
      switch (v.getId()) {
       case R.id.but1:                 // your button id
           intent = new Intent(Activity1.this,Activity2.class);         
          break;
       // -- so on
      }
    startActivity(intent);
    finish();
}

将您的所有活动意图放在2,3,4案例中。

答案 2 :(得分:0)

您需要在View.OnClickListenerMainActivity方法中实施onClick

@Override
    public void onClick(View v) {
        Intent intent;
        switch(v.getId()){
            case R.id.btn_1:
                 intent = new Intent(MainActivity.this,YourClass1.class);
                startActivity(intent);
                break;
            case R.id.bt_2:
                intent = new Intent(MainActivity.this,YourClass2.class);
                startActivity(intent);
                break;
            case R.id.bt_3:
                intent = new Intent(MainActivity.this,YourClass3.class);
                startActivity(intent);
                break;
            case R.id.bt_4:
                intent = new Intent(MainActivity.this,YourClass4.class);
                startActivity(intent);
                break;
        }
    }

onCreate() MainActivity中的button方法必须将clickListener设置为evry @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn1= (Button) findViewById(R.id.bt_1); btn2= (Button) findViewById(R.id.bt_2); btn3= (Button) findViewById(R.id.bt_3); btn4= (Button) findViewById(R.id.bt_4); btn1.setOnClickListener(this); btn2.setOnClickListener(this); btn3.setOnClickListener(this); btn4.setOnClickListener(this); } ,如下所示:

SKErrorClientInvalid
SKErrorPaymentCancelled
SKErrorPaymentInvalid
SKErrorPaymentNotAllowed
SKErrorStoreProductNotAvailable
SKErrorUnknown