Buttonの簡単な例1

13564 ワード

1.XMLボタン定義
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent" >



<Button

    android:id="@+id/button1"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_marginLeft="50dp"

    android:layout_marginRight="50dp"

    android:text="        "

    android:textColor="#ff0000" />



<Button

    android:id="@+id/button2"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:layout_marginTop="100dp"

    android:background="#0000ff"

    android:fontFamily="  "

    android:text="       "

    android:textColor="#ffffff" />



</RelativeLayout>
 表示:
Button简单实例1
2.バックグラウンドコードがコントロールを作成し、イベントを登録する
import android.annotation.SuppressLint;

import android.app.Activity;

import android.app.ActionBar.LayoutParams;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.LinearLayout;



/**

 *         ,     

 * 

 * @author zhongyang

 *

 */

public class Button_Two extends Activity {

    private LinearLayout parentLayout = null;



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        //       

        createParentLayout();

        //     

        CreateButton();

    }



    //       

    @SuppressLint("NewApi")

    private void createParentLayout() {

        parentLayout = new LinearLayout(this);

        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,

                LayoutParams.MATCH_PARENT);

        parentLayout.setLayoutParams(layoutParams);

        setContentView(parentLayout);

    }



    //          

    @SuppressLint("NewApi")

    private void CreateButton() {

        Button btnOne = new Button(this);

        btnOne.setText("    Activity");

        btnOne.setOnClickListener(new btnOneListener());



        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,

                LayoutParams.WRAP_CONTENT);

        btnOne.setLayoutParams(layoutParams);



        parentLayout.addView(btnOne);

    }



    //         

    class btnOneListener implements View.OnClickListener {

        public void onClick(View v) {

            Button thisBtn = (Button) v;

            thisBtn.setWidth(100);

            thisBtn.setText("         ");

        }

    }

}
表示:
Button简单实例1
3.XML作成コントロール登録clickイベント
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical" >



<TextView

    android:id="@+id/txtOne"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:layout_marginTop="30px"

    android:background="#000000"

    android:paddingBottom="20px"

    android:paddingLeft="20px"

    android:paddingTop="20px"

    android:text="      "

    android:textColor="#ffffff" />



<Button

    android:id="@+id/btnOne"

    android:layout_width="100dp"

    android:layout_height="50dp"

    android:layout_marginTop="30px"

    android:onClick="btnOneClick"

    android:text="  One"

    android:textColor="#00ff00"

    android:textSize="20px" />



</LinearLayout>
import android.app.Activity;

import android.content.res.ColorStateList;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

/**

 *                

 * @author zhongyang

 *

 */

public class ButtonThree extends Activity {

    private TextView txtOne = null;

    private Button btnOne = null;



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        //

        setContentView(R.layout.button_three);

        txtOne = (TextView) this.findViewById(R.id.txtOne);

        btnOne = (Button) this.findViewById(R.id.btnOne);

    }



    //       

    public void btnOneClick(View view) {

        Button btn = (Button) view;

        btn.setTextSize(25);



        //   TextView  

        txtOne.setText("  One      ");

    }

}
表示:
Button简单实例1