-4

我知道如何将单个按钮添加到 XML、Java 和 Manifest,但我不知道如何添加多个按钮。您能帮我编写 Java 文件和清单的代码吗?

4

2 回答 2

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;
       }
    }
}
于 2013-01-28T06:46:10.253 回答
-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);

   }
}
于 2013-01-28T06:57:36.657 回答