您好,登錄后才能下訂單哦!
xml中的代碼實(shí)現(xiàn)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="20dp" > <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/title" /> <EditText android:id="@+id/phonenumber" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="請(qǐng)輸入手機(jī)號(hào)碼" android:inputType="phone" android:layout_below="@id/title" /> <Button android:id="@+id/but" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="call" android:text="撥打" android:layout_below="@id/phonenumber" /> </RelativeLayout>
mainActivity中代碼的實(shí)現(xiàn)
public class MainActivity extends Activity { private EditText edit; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); edit=(EditText)findViewById(R.id.phonenumber); } public void call(View v){ System.out.println("被點(diǎn)擊了"); String num=edit.getText().toString(); Intent intent=new Intent();//創(chuàng)建一個(gè)意圖 intent.setAction(intent.ACTION_CALL);//指定動(dòng)作為撥打電話 intent.setData(Uri.parse("tel:"+num));//指定要播出的電話號(hào)碼 startActivity(intent); } }
同時(shí)還需要給我們的應(yīng)用程序添加一個(gè)可以撥打號(hào)碼的權(quán)限
<!-- 添加撥打電話的權(quán)限 --> <uses-permission android:name="android.permission.CALL_PHONE" />
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。