android Button點擊事件總結

來源:互聯網
上載者:User

標籤:wrap   schema   效果   xmlns   nts   override   new   height   div   

直接上代碼:

public class MainActivity extends AppCompatActivity implements View.OnClickListener{    Button button1,button2,button3,button4,button5,button6;    TextView text1;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        button1=(Button)findViewById(R.id.button1);        button2=(Button)findViewById(R.id.button2);        button3=(Button)findViewById(R.id.button3);        button4=(Button)findViewById(R.id.button4);        //按鈕5和按鈕6的點擊事件是在布局檔案中添加的        button5=(Button)findViewById(R.id.button5);        button6=(Button)findViewById(R.id.button6);        text1=(TextView)findViewById(R.id.text1);        // 第一個按鈕        button1.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v){                text1.setText("按鈕1");            }        });        //第二個按鈕        button2.setOnClickListener(list);        //第三個按鈕建立類繼承點擊事件        button3.setOnClickListener(new mm());        //第四個按鈕本身繼承點擊事件        button4.setOnClickListener(this);    }    Button.OnClickListener list=new Button.OnClickListener(){        @Override        public void onClick(View v) {            text1.setText("按鈕2");        }    };    class mm implements View.OnClickListener{        @Override        public void onClick(View v) {            text1.setText("按鈕3");        }    }    @Override    public void onClick(View v) {        text1.setText("按鈕4");    }    public void click(View v){        int id=v.getId();        switch (id){            case R.id.button5:                text1.setText("按鈕5");                break;            case R.id.button6:                text1.setText("按鈕6");                break;        }    }}

布局檔案代碼:

<LinearLayout    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent"    xmlns:android="http://schemas.android.com/apk/res/android">    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/button1"        android:text="按鈕1"        android:layout_gravity="center"        />    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/button2"        android:text="按鈕2"        android:layout_gravity="center"        />    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/button3"        android:text="按鈕3"        android:layout_gravity="center"        />    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/button4"        android:text="按鈕4"        android:layout_gravity="center"        />    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/button5"        android:text="按鈕5"        android:onClick="click"        android:layout_gravity="center"        />    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/button6"        android:text="按鈕6"        android:onClick="click"        android:layout_gravity="center"        />    <TextView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:id="@+id/text1"        android:textColor="#FF0000"        android:textSize="30dp"        android:gravity="center"        android:text="目前沒有點擊"        /></LinearLayout>

 :

android Button點擊事件總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.