如何在eclipse中添加多个按钮

时间:2013-01-28 06:38:42

标签: java android button android-manifest

我知道如何向XML,Java和Manifest添加单个按钮,但我不知道如何添加多个按钮。你能帮我解决Java文件和Manifest的代码吗?

2 个答案:

答案 0 :(得分:0)

这是在java中添加多个按钮的方法:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class TestActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    Button button1, button2, button3;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        button1 = (Button)findViewById(R.id.button1);
        button2 = (Button)findViewById(R.id.button2);
        button3 = (Button)findViewById(R.id.button3);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
    }

    public void onClick(View v) {
        //figure out which button was pressed:
        switch (v.getId()) {
        case R.id.button1:
            //do button1 actions
            break;
        case R.id.button2:
            //do button2 actions
            break;
        case R.id.button3:
            //do button3 actions
            break;
       }
    }
}

答案 1 :(得分:-1)

public class TestActivity1 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
   Button  button1 = (Button)findViewById(R.id.button1);
    Button  button2 = (Button)findViewById(R.id.button2);

   }
}