Button を使ってみた

Buton は
java.lang.Object
  ↳ android.view.View
    ↳ android.widget.TextView
      ↳ android.widget.Button
を使用します

というわけで、いろいろと値を指定して Button をいじってみた

サンプルプログラム
  1. import android.app.Activity;  
  2. import android.graphics.Color;  
  3. import android.os.Bundle;  
  4. import android.view.View;  
  5. import android.widget.Button;  
  6.   
  7. public class ButtonTest extends Activity {  
  8.   Button button1;  
  9.   Button button2;  
  10.   @Override  
  11.   public void onCreate(Bundle savedInstanceState) {  
  12.     super.onCreate(savedInstanceState);  
  13.     setContentView(R.layout.buttontest);  
  14.   
  15.     button1 = (Button) findViewById(R.id.Button01);  
  16.     button2 = (Button) findViewById(R.id.Button02);  
  17.   
  18.     // Button がクリックされた時に呼び出されるコールバックを登録  
  19.     button1.setOnClickListener(new View.OnClickListener() {  
  20.       public void onClick(View v) {  
  21.         // Button のテキストを設定  
  22.         button1.setText("Clicked!");  
  23.   
  24.         // Button のバックグラウンドのカラーを設定  
  25.         button1.setBackgroundColor(Color.RED);  
  26.       }  
  27.     });  
  28.   
  29.     // Button のバックグラウンドのリソースを設定  
  30.     button2.setBackgroundResource(R.drawable.icon);  
  31.   }  
  32. }  

XMLリソース
  1. <button  
  2.   android:id="@+id/Button"  
  3.   android:layout_height="wrap_content"  
  4.   android:layout_width="wrap_content"  
  5.   android:text="Button">  
  6. </button>  

プログラムを実行すると...


クリック前

クリック後

こんな感じ!

0 件のコメント:

コメントを投稿