Android安卓的四種點擊事件

這裏以點擊按鈕撥打電話爲例:

第一種:在Activity中自定義了一個OnClickListener的實現類;

第二種:在button.setOnClickListener()方法中寫一個內部類;

第三種:讓Activity實現OnClickListener接口,實現onClick方法;

第四種:在佈局文件中,給按鈕設置onClick屬性,然後在Activity中寫方法(public void call(View v));

 (4種方法各有優劣,在實際使用過程中根據具體情況進行選擇使用)

1、內部類的方法:

(1)在佈局文件中定義按鈕的ID:

    <Button

     android:layout_width="match_parent"

     android:layout_height="wrap_content"

    android:text="撥打"

    android:id="@+id/bt"

    />

(2)在代碼中給按鈕添加監聽事件:

    private EditText et_phone;

    private Button bt;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        //初始化控件

        et_phone = (EditText) findViewById(R.id.et_phone);

        bt = (Button) findViewById(R.id.bt);

        bt.setOnClickListener(new OnClickListener() {

            //點擊按鈕之後纔會調用這個方法

            @Override

            public void onClick(View v) {

                String phone = et_phone.getText().toString().trim();

                if(TextUtils.isEmpty(phone)){

                    Toast.makeText(MainActivity.this, "電話號碼不能爲空", Toast.LENGTH_SHORT).show();

                }else{

                    //撥打電話號碼

                    Intent intent = new Intent();

                    //設置 撥打電話的動作

                    intent.setAction(Intent.ACTION_CALL);

                    //設置數據

                    intent.setData(Uri.parse("tel://"+phone));

                    //調用撥打電話號碼的功能

                    startActivity(intent);

                }

            }

        });

    }

2、自定義一個監聽器類:

    private class MyListener implements OnClickListener{

    @Override

    public void onClick(View v) {

        //點擊按鈕之後纔會調用這個方法

            String phone = et_phone.getText().toString().trim();

            if(TextUtils.isEmpty(phone)){

                Toast.makeText(MainActivity.this, "電話號碼不能爲空", Toast.LENGTH_SHORT).show();

            }else{

                //撥打電話號碼

                Intent intent = new Intent();

                //設置 撥打電話的動作

                intent.setAction(Intent.ACTION_CALL);

                //設置數據

                intent.setData(Uri.parse("tel://"+phone));

                //調用撥打電話號碼的功能

                startActivity(intent);

            }

    }

}

3、在佈局文件中給按鈕添加一個單擊事件的響應方法,然後再中添加一個方法:

    佈局文件:

        <Button

         android:layout_width="match_parent"

         android:layout_height="wrap_content"

        android:text="撥打"

        //定義方法

        android:onClick="call"

        />

    代碼:

        public void call(View view){

        String phone = et_phone.getText().toString().trim();

        if(TextUtils.isEmpty(phone)){

            Toast.makeText(this, "電話號碼不能爲空", Toast.LENGTH_SHORT).show();

        }else{

            //撥打電話號碼

            Intent intent = new Intent();

            //設置 撥打電話的動作

            intent.setAction(Intent.ACTION_CALL);

            //設置數據

            intent.setData(Uri.parse("tel://"+phone));

            //調用撥打電話號碼的功能

            startActivity(intent);

        }

    }

4、是activity實現OnClickListener接口,重新onClick方法:

    public class MainActivity extends Activity implements OnClickListener{

        private EditText et_phone;

        private Button bt;

        @Override

        protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_main);

            //初始化控件

            et_phone = (EditText) findViewById(R.id.et_phone);

            bt = (Button) findViewById(R.id.bt);

            //添加單擊事件的監聽器

            bt.setOnClickListener(this);

        }

        //實現接口的方法

        public void onClick(View v) {

            String phone = et_phone.getText().toString().trim();

            if(TextUtils.isEmpty(phone)){

                Toast.makeText(MainActivity.this, "電話號碼不能爲空", Toast.LENGTH_SHORT).show();

            }else{

                //撥打電話號碼

                Intent intent = new Intent();

                //設置 撥打電話的動作

                intent.setAction(Intent.ACTION_CALL);

                //設置數據

                intent.setData(Uri.parse("tel://"+phone));

                //調用撥打電話號碼的功能

                startActivity(intent);

        }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章