[Android入門]ボタン(Button)を使用する方法

1.一つボタン
public class DemoActivity extends Activity {
private Button button = (Button) findViewById(R.id.button_id);

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
}
}

2.複数ボタン
public class DemoActivity extends Activity implements OnClickListener {

private Button Btn1, Btn2;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

//UI initialization
setupUI();
}
public void setupUI() {
Btn1 = (Button) findViewById(R.id.button1);
Btn2 = (Button) findViewById(R.id.button2);

Btn1.setOnClickListener(this);
Btn2.setOnClickListener(this);
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {

case R.id.button1:
onFn1();
break;
case R.id.button2:
onFn2();
break;
}

}

Android

Posted by arkgame