''[[MobileApp>Mobile Application 入門]]'' > ''[[Android>Android]]'' > ''[[GUI部品>Android GUI部品]]'' > ''ボタン'' ~ ~ ~ *ボタンの表示 [#wd988f5b] ボタンの実現イメージ #ref(button.gif); ~ ~ ~ *ボタンの属性 [#md685236] [[Buttonの属性>Android Buttonの属性]] ~ ~ *サンプルソース [#e835b633] **ボタンを配置&クリック認識をさせる際のフォーマット (Layoutのxmlを使用する場合) [#td0adbb6] ・クリック処理の記述方法は、setOnClickListenerでリスナーを登録した上で、~ onClick(View v)で ボタン押下時の処理を記述する ・Listener使用には「import android.view.View.*;」の宣言が必要~ ・onClick(View v)の使用には「import android.view.*;」の宣言が必要~ ・Buttonの使用には「import android.widget.*;」の宣言が必要~ ~ 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); // 指定されたID(この場合はボタンのID)からビューを探す button1 = (Button) findViewById(R.id.button_id); // ボタンがクリックされた時に呼び出されるコールバック登録 button1.setOnClickListener(this); // 指定されたID(この場合はボタンのID)からビューを探す 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> ~ **ボタンを配置するソースフォーマット (ソースにレイアウトを直書きの場合) [#td0adbb6] import android.app.Activity; import android.os.Bundle; &color(red,){ import android.widget.Button;}; import android.widget.Button; public class Test extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Button button = new Button(this); button.setText("button"); setContentView(button); } } ~ ~ *参考 [#a3c90ec3] [[日本Androidの会>http://android.siprop.org/index.php?%CA%D9%B6%AF%B2%F1%2F%A5%DC%A5%BF%A5%F3%A4%F2%A4%C4%A4%AF%A4%C3%A4%C6%A4%DF%A4%E8%A4%A6]] ~ ~ ~ ''[[戻る>Android GUI部品]]''