xml中的代碼實現
<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="請輸入手機號碼" 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中代碼的實現
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("被點擊了");
String num=edit.getText().toString();
Intent intent=new Intent();//創建一個意圖
intent.setAction(intent.ACTION_CALL);//指定動作為撥打電話
intent.setData(Uri.parse("tel:"+num));//指定要播出的電話號碼
startActivity(intent);
}
}同時還需要給我們的應用程序添加一個可以撥打號碼的權限
<!-- 添加撥打電話的權限 --> <uses-permission android:name="android.permission.CALL_PHONE" />
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。