java.lang.Object ↳ android.view.View ↳ android.widget.TextView ↳ android.widget.Buttonを使用します
というわけで、いろいろと値を指定して Button をいじってみた
サンプルプログラム
- import android.app.Activity;
- import android.graphics.Color;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- public class ButtonTest extends Activity {
- Button button1;
- Button button2;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.buttontest);
- button1 = (Button) findViewById(R.id.Button01);
- button2 = (Button) findViewById(R.id.Button02);
- // Button がクリックされた時に呼び出されるコールバックを登録
- button1.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- // Button のテキストを設定
- button1.setText("Clicked!");
- // Button のバックグラウンドのカラーを設定
- button1.setBackgroundColor(Color.RED);
- }
- });
- // Button のバックグラウンドのリソースを設定
- button2.setBackgroundResource(R.drawable.icon);
- }
- }
XMLリソース
- <button
- android:id="@+id/Button"
- android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:text="Button">
- </button>
プログラムを実行すると...
こんな感じ!
0 件のコメント:
コメントを投稿