日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > Android >内容正文

Android

Android入门——电话拨号器和四种点击事件

發(fā)布時(shí)間:2023/12/20 Android 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android入门——电话拨号器和四种点击事件 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

相對(duì)于HelloWorld來說,電話撥號(hào)器也是Android的一個(gè)入門demo,從這個(gè)樣例我們要理清楚做安卓項(xiàng)目的思路。

?大體分為三步:

????1.理解需求,理清思路

????2.設(shè)計(jì)UI

????3.代碼實(shí)現(xiàn)

電話撥號(hào)器

1.?理解需求:

????*一個(gè)文本框——用來接收電話號(hào)碼

????*一個(gè)button——用來觸發(fā)事件

2.?設(shè)計(jì)UI

? ? ?

3.?代碼實(shí)現(xiàn)

public class MainActivity extends Activity {private EditText et_number; //定義變量用來接收電話號(hào)碼private Button btn; //定義button@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//設(shè)置界面內(nèi)容setContentView(R.layout.activity_main);//查找控件,文本輸入框et_number= (EditText) findViewById(R.id.et_number);//找到按鈕控件btn=(Button) findViewById(R.id.btn);//設(shè)置點(diǎn)擊事件監(jiān)聽btn.setOnClickListener(new MyListener());}//內(nèi)部類方式實(shí)現(xiàn)點(diǎn)擊事件class MyListener implements OnClickListener{@Overridepublic void onClick(View v) {//運(yùn)行撥號(hào)操作//1.獲取用戶輸入的號(hào)碼String number=et_number.getText().toString();//2.運(yùn)行撥號(hào)操作//創(chuàng)建一個(gè)撥號(hào)意圖Intent intent=new Intent();//設(shè)置要撥打的號(hào)碼 (URL:統(tǒng)一資源定位符,uri:統(tǒng)一資源標(biāo)識(shí)符)intent.setData(Uri.parse("tel://"+number));//設(shè)置動(dòng)作,撥號(hào)動(dòng)作intent.setAction(intent.ACTION_CALL);//跳轉(zhuǎn)到撥號(hào)界面startActivity(intent);} } }

代碼寫完以后。從配置文件里加入一下打電話權(quán)限CALL_PHONE就OK了。

上面的電話撥號(hào)器用了一種實(shí)現(xiàn)點(diǎn)擊事件的方式。也是要介紹的第一種點(diǎn)擊事件的方法:

四種點(diǎn)擊事件實(shí)現(xiàn)方式

1.內(nèi)部類方式

? ? ?(見上面電話撥號(hào)demo,只是多介紹)

2.創(chuàng)建匿名內(nèi)部類方式

/*** 匿名內(nèi)部類方式實(shí)現(xiàn)點(diǎn)擊事件* @author Hugh*/ public class MainActivity extends Activity {private EditText et_number;private Button btn;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//設(shè)置界面內(nèi)容setContentView(R.layout.activity_main);//查找控件,文本輸入框et_number= (EditText) findViewById(R.id.et_number);//找到button控件btn=(Button) findViewById(R.id.btn);//設(shè)置點(diǎn)擊事件監(jiān)聽btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) { //運(yùn)行撥號(hào)操作//1.獲取用戶輸入的號(hào)碼String number=et_number.getText().toString();//2.運(yùn)行撥號(hào)操作//意圖Intent intent=new Intent();//設(shè)置要撥打的號(hào)碼intent.setData(Uri.parse("tel://"+number));//設(shè)置動(dòng)作,撥號(hào) 動(dòng)作intent.setAction(intent.ACTION_CALL);//跳轉(zhuǎn)到撥號(hào)界面startActivity(intent);}});} }

3.實(shí)現(xiàn)OnClickListener接口的當(dāng)前類對(duì)象

/*** 實(shí)現(xiàn)OnClickListener接口的當(dāng)前類對(duì)象* @author Hugh*/ public class MainActivity extends Activity implements OnClickListener {private EditText et_number;private Button btn;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// 設(shè)置界面內(nèi)容setContentView(R.layout.activity_main);// 查找控件,文本輸入框et_number = (EditText) findViewById(R.id.et_number);// 找到button控件btn = (Button) findViewById(R.id.btn);// 設(shè)置點(diǎn)擊事件監(jiān)聽btn.setOnClickListener(this);}@Overridepublic void onClick(View v) {// 運(yùn)行撥號(hào)操作// 1.獲取用戶輸入的號(hào)碼String number = et_number.getText().toString();// 2.運(yùn)行撥號(hào)操作// 意圖Intent intent = new Intent();// 設(shè)置要撥打的號(hào)碼intent.setData(Uri.parse("tel://" + number));// 設(shè)置動(dòng)作,撥號(hào) 動(dòng)作intent.setAction(intent.ACTION_CALL);// 跳轉(zhuǎn)到撥號(hào)界面startActivity(intent);} }

4.在布局文件里給button加入一個(gè)單擊事件的響應(yīng)方法,然后在代碼中實(shí)現(xiàn)這種方法

/短信群發(fā)器/res/layout/activity_main.xml 下定義xml中的屬性android:onClick="call"<Buttonandroid:id="@+id/btn"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignLeft="@+id/et_number"android:layout_below="@+id/et_number"android:onClick="call"android:text="按下?lián)芴?hào)" />

在代碼中實(shí)現(xiàn)這種方法

public class MainActivity extends Activity {private EditText et_number;private Button btn;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// 設(shè)置界面內(nèi)容setContentView(R.layout.activity_main);// 查找控件,文本輸入框et_number = (EditText) findViewById(R.id.et_number);}public void call(View view){;// 運(yùn)行撥號(hào)操作// 1.獲取用戶輸入的號(hào)碼String number = et_number.getText().toString();// 2.運(yùn)行撥號(hào)操作// 意圖Intent intent = new Intent();// 設(shè)置要撥打的號(hào)碼intent.setData(Uri.parse("tel://" + number));// 設(shè)置動(dòng)作,撥號(hào) 動(dòng)作intent.setAction(intent.ACTION_CALL);// 跳轉(zhuǎn)到撥號(hào)界面startActivity(intent);} }
? ?對(duì)于Android的學(xué)習(xí),須要不斷的積累。砸實(shí)基礎(chǔ)。

這篇博客算是個(gè)開始吧,我要把博客又一次拾起來,加油!





總結(jié)

以上是生活随笔為你收集整理的Android入门——电话拨号器和四种点击事件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。