android 13 5種click事件不同實現方式 比較

來源:互聯網
上載者:User

標籤:

第一種:不便於管理。

<Button        android:id="@+id/btn_Gridlayout"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="用網格布局實現註冊"         android:onClick="gridLayout"/>
public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {//視圖載入後第一個調用的方法,Activity自動建立,不能new,可以調用方法啟動他,        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);//設定Activity的布局,顯示Activity的布局        findViewById(R.id.rbFemale);//在R這個類的id這個內部類找這個值,然後找到這個id標識的東西,傳回值是View    }    //onClick值是gridLayout的按鈕的點擊事件響應的方法    public void gridLayout(View v){//v是點的按鈕,view是所有控制項的父類        Log.i("main",((Button)v).getText().toString());        setContentView(R.layout.gridlayout);    }}

第二種:匿名內部類,如果只是用一次則用這種,這不便於代碼的複用。

第三種:有名內部類,代碼有複用性。

第四種:Activity實現click介面,

 public void onClick(View v) {//View是所有布局的基類        switch (v.getId()) {        case R.id.btnLogin:            Toast.makeText(this, "執行登陸操作", 2000).show();            break;        case R.id.btnExit:            finish();            break;        }    }
用於按鈕很多,而且每個點擊事件的代碼不多的時候用,如果代碼很多,把swith變的很大就不適合用了。
第五種:外部類,具有複用性,有利於代碼的混淆。

android 13 5種click事件不同實現方式 比較

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.