Android实现点击事件的4种方式
一、通過(guò)在activity_main.xml中,按鈕button控件中添加onclick事件實(shí)現(xiàn)
在?? activity_main.xml 對(duì)應(yīng)的按鈕Button中加入下面紅色事件
<Button android:id="@+id/send_button"
??????? android:layout_width="wrap_content"
??????? android:layout_below="@id/content_edit"
??????? android:text="@string/send_button"
??????? android:onClick="call"
??????? android:layout_height="wrap_content"/>
在布局對(duì)應(yīng)的Activity,即MainActivity.java中加入下面方法,下面是點(diǎn)擊撥打號(hào)碼的時(shí)候的相關(guān)操作
? /**
???? * 當(dāng)撥打此號(hào)碼的按鈕被點(diǎn)擊時(shí)觸發(fā)此方法.
???? * @param v
???? */
??? public void call(View v) {
??????? System.out.println("撥打電話……….");
???????
??????? // 1. 取出輸入框中的號(hào)碼
??????? EditText etNumber = (EditText) findViewById(R.id.number);??? // 輸入框?qū)ο?br />??????? String number = etNumber.getText().toString();??? // 將要撥打的號(hào)碼
???????
??????? // 2. 根據(jù)號(hào)碼撥打電話
??????? Intent intent = new Intent();??????? // 創(chuàng)建一個(gè)意圖
??????? intent.setAction(Intent.ACTION_CALL);??????? // 指定其動(dòng)作為撥打電話
??????? intent.setData(Uri.parse("tel:" + number));??? // 指定將要撥出的號(hào)碼
??????? startActivity(intent);??? // 執(zhí)行這個(gè)動(dòng)作
??? }
?
二、采用匿名內(nèi)部類(lèi)的方式,在布局初始化的時(shí)候添加事件監(jiān)聽(tīng)
在 activity_main.xml? 不要添加onclick事件
在布局對(duì)應(yīng)的Activity,即MainActivity.java的onCreate方法中,添加下面代碼實(shí)現(xiàn)事件監(jiān)聽(tīng):
??????? Button btnCall = (Button) findViewById(R.id.btn_call);
??????? btnCall.setOnClickListener(new OnClickListener() {
??????????? @Override
??????????? public void onClick(View v) {
??????????????? System.out.println("MainUI2 撥打電話.");
??????????????? call();
??????????? }
??????? });
?
三、采用內(nèi)部類(lèi)的方式,在布局初始化的時(shí)候添加事件監(jiān)聽(tīng)
在 activity_main.xml? 不要添加onclick事件
在布局對(duì)應(yīng)的Activity,即MainActivity.java的onCreate方法中,添加下面代碼實(shí)現(xiàn)事件監(jiān)聽(tīng):
protected void onCreate(Bundle savedInstanceState) {
??????? super.onCreate(savedInstanceState);??????? // 必須執(zhí)行此句代碼. 執(zhí)行父類(lèi)的初始化操作.
??????? setContentView(R.layout.main);??????????????? // 設(shè)置當(dāng)前界面顯示的布局.
??????? Button btnCall = (Button) findViewById(R.id.btn_call);
??????? btnCall.setOnClickListener(new MyOnClickListener());
??? }
???
class MyOnClickListener implements OnClickListener {
??????? @Override
??????? public void onClick(View v) {
??????????? System.out.println("撥打號(hào)碼..");
??????????? call();
??????? }
??? }
???
??? /**
???? * 撥打電話的業(yè)務(wù)方法
???? */
??? private void call() {
?????????????? //實(shí)現(xiàn)事件觸發(fā)的要執(zhí)行的邏輯??
?? }
?
四、采用Activity實(shí)現(xiàn)接口的方式,實(shí)現(xiàn)事件監(jiān)聽(tīng)
在 activity_main.xml? 不要添加onclick事件,? 讓布局Activity實(shí)現(xiàn)接口OnClickListener,并實(shí)現(xiàn)接口的方法
?
public class MainActivity extends Activity implements OnClickListener
?
private void call() {
??????? // 1. 取出輸入框中的號(hào)碼
??????? EditText etNumber = (EditText) findViewById(R.id.number);??? // 輸入框?qū)ο?br />??????? String number = etNumber.getText().toString();??? // 將要撥打的號(hào)碼
???????
??????? // 2. 根據(jù)號(hào)碼撥打電話
??????? Intent intent = new Intent();??????? // 創(chuàng)建一個(gè)意圖
??????? intent.setAction(Intent.ACTION_CALL);??????? // 指定其動(dòng)作為撥打電話
??????? intent.setData(Uri.parse("tel:" + number));??? // 指定將要撥出的號(hào)碼
??????? startActivity(intent);??? // 執(zhí)行這個(gè)動(dòng)作
??? }
??? @Override
??? public void onClick(View v) {
??????? call();
??? }
轉(zhuǎn)載于:https://www.cnblogs.com/zhongjianlong/p/3991502.html
總結(jié)
以上是生活随笔為你收集整理的Android实现点击事件的4种方式的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 敏捷之旅杭州站演讲PPT(敏捷开发在淘女
- 下一篇: 看来我的计时器的应用还要加强才行呀