Android入门——电话拨号器和四种点击事件
生活随笔
收集整理的這篇文章主要介紹了
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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 笨办法学C 练习6:变量类型
- 下一篇: MySQL如何发型不乱的应对半年数十TB