戻る?
クリックはsetOnClickListenerでリスナーを登録した上で、
onClick(View v)で ボタン押下時の処理を記述
import android.app.Activity; import android.os.Bundle; import android.widget.*; import android.view.*; import android.view.View.*; public class TestTest extends Activity implements OnClickListener{ private Button button1; private Button button2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); button1 = (Button) findViewById(R.id.button_id); // ボタンがクリックされた時に呼び出されるコールバック登録 button1.setOnClickListener(this); button2 = (Button) findViewById(R.id.button_id); // ボタンがクリックされた時に呼び出されるコールバック登録 button2.setOnClickListener(this); } public void onClick(View v) { // ボタンがクリックされた時に呼び出される //Button button = (Button) v; //ボタンを押した時の処理 if (v == button1){ 処理1 }else if (v == button2){ 処理2 } } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <!-- ボタンの定義。android:idを定義することでJavaから取り出せる --> <Button android:id="@+id/button_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button_label" /> </LinearLayout>
import android.app.Activity; import android.os.Bundle; import android.widget.Button; public class Test extends Activity { protected void onCreate(Bundle icicle) { super.onCreate(icicle); Button button = new Button(this); button.setText("button"); setContentView(button); } }
戻る?