如何在前一个旁边添加另一个按钮意图?

时间:2013-01-26 07:36:15

标签: android eclipse button android-intent

这是我的代码:  它包含一个名为 button1A 的按钮,当我点击它时,它会打开一个名为 list1 的列表。 如何为我的另一个名为button2A的按钮添加代码,该按钮将列表打开为List2。

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;

import android.widget.Button;

public class Tab_1st extends Activity {

Button button1A;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tablayout1);

    addListenerOnButton();


}

public void addListenerOnButton() {

    button1A = (Button) findViewById(R.id.button1A);

    button1A.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent a1 = new Intent(v.getContext(), List1.class);
            startActivity(a1);

        }
    });

}

}

3 个答案:

答案 0 :(得分:1)

使用addListenerOnButton更改switch-case方法以最小化代码并将单个OnClickListener侦听器添加到多个按钮:

    public void addListenerOnButton() {

        button1A = (Button) findViewById(R.id.button1A);
        button1A.setOnClickListener(clicklistener);
        // add OnClickListener to second Button 
        button1B = (Button) findViewById(R.id.button1B);
        button1B.setOnClickListener(clicklistener);
    OnClickListener clicklistener = new View.OnClickListener() {
     @Override
      public void onClick(View v) {
        switch(v.getId()){

          case R.id.button1A: 
                    // start second List Activity
               Intent a1 = new Intent(v.getContext(), List1.class);
               startActivity(a1);
               break;

          case R.id.button2A: 
                     // start second List Activity
               Intent a2 = new Intent(v.getContext(), List2.class);
               startActivity(a2);
               break;

        }
       }
     };
   }

答案 1 :(得分:0)

如果需要,您可以添加更多点击侦听器。试试这样。

public void addListenerOnButton() {

button1A = (Button) findViewById(R.id.button1A);

button1A.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

        Intent a1 = new Intent(v.getContext(), List1.class);
        startActivity(a1);

    }
});

button2A = (Button) findViewById(R.id.button2A);

button2A.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

        Intent i = new Intent(v.getContext(), List2.class);
        startActivity(i);

    }
});

}

答案 2 :(得分:0)

这样做的一种方法是实现OnClickListener。

public class Tab_1st extends Activity implements OnClickListener {

Button button1A;
Button button2A;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tablayout1);
    button1A = (Button) findViewById(R.id.button1A);
    button2A = (Button) findViewById(R.id.button2A);
    button1A.setOnClickListener(this);
    button2A.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    if (v == button1A) {
        // Start Activity a1
        Intent a1 = new Intent(v.getContext(), List1.class);
        startActivity(a1);
    } else if (v == button2A) {
        // Start Activity a2
        Intent a2 = new Intent(v.getContext(), List2.class);
        startActivity(a2);
    }

}

}