[HP Top] |
MobileApp > Android > GUI部品 > ボタン
ボタンの表示 †ボタンの実現イメージ
ボタンの属性 †
サンプルソース †ボタンを配置&クリック認識をさせる際のフォーマット (Layoutのxmlを使用する場合) † ・クリック処理の記述方法は、setOnClickListenerでリスナーを登録した上で、 ・Listener使用には「import android.view.View.*;」の宣言が必要 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> ボタンを配置するソースフォーマット (ソースにレイアウトを直書きの場合) †import android.app.Activity; import android.os.Bundle; 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); } }
参考 † |