(Android 基礎知識review)打電話

來源:互聯網
上載者:User

(Android 基礎知識review)打電話

1、main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >        <TextView         android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/a"        />        <EditText         android:id="@+id/num_et"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:hint="@string/c"        />        <Button         android:id="@+id/call_bt"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/b"        /></LinearLayout>


2、MainActivity

package com.njupt.phonetest;import android.net.Uri;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;public class MainActivity extends Activity {private EditText et_num;private Button bt_call;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);et_num = (EditText) findViewById(R.id.num_et);bt_call = (Button) findViewById(R.id.call_bt);bt_call.setOnClickListener(new MyOnClickListener());}private class MyOnClickListener implements OnClickListener{@Overridepublic void onClick(View v) {String phoneNum = et_num.getText().toString();/** * 以下是打電話的核心代碼: */Intent intent = new Intent();//建立意圖對象intent.setAction(Intent.ACTION_CALL);//設定意圖的動作(撥打到電話)intent.setData(Uri.parse("tel:" + phoneNum));//設定意圖的資料(電話號碼)startActivity(intent);//使用意圖開啟一個介面(打電話的介面)}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}

3、string.xml

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="app_name">PhoneTest</string>    <string name="action_settings">Settings</string>    <string name="hello_world">Hello world!</string>    <string name="a">請輸入電話號碼</string>    <string name="b">撥打號碼</string>    <string name="c">請在這裡輸入號碼</string></resources>


4、AndroidManifest.xml

在這個設定檔中加上:

<uses-permission android:name="android.permission.CALL_PHONE"/>


---------------------------以上用到的時候點擊事件的處理方式的第一種。以下介紹事件處理的第二種、第三種處理方式------------


第二種事件處理方式:

1、MainActivity2

package com.njupt.phonetest;import android.app.Activity;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;public class MainActivity2 extends Activity implements OnClickListener{private EditText et_num;private Button bt_call;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);et_num = (EditText) findViewById(R.id.num_et);bt_call = (Button) findViewById(R.id.call_bt);bt_call.setOnClickListener(this);}public void onClick(View v) {String phoneNum = et_num.getText().toString();/** * 以下是打電話的核心代碼: */Intent intent = new Intent();// 建立意圖對象intent.setAction(Intent.ACTION_CALL);// 設定意圖的動作(撥打到電話)intent.setData(Uri.parse("tel:" + phoneNum));// 設定意圖的資料(電話號碼)startActivity(intent);// 使用意圖開啟一個介面(打電話的介面)}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}

其他的代碼不需要改變



第三種事件處理的方式:


1、MainActivity3

package com.njupt.phonetest;import android.app.Activity;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;public class MainActivity3 extends Activity {private EditText et_num;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);et_num = (EditText) findViewById(R.id.num_et);}public void hello(View v) {String phoneNum = et_num.getText().toString();/** * 以下是打電話的核心代碼: */Intent intent = new Intent();// 建立意圖對象intent.setAction(Intent.ACTION_CALL);// 設定意圖的動作(撥打到電話)intent.setData(Uri.parse("tel:" + phoneNum));// 設定意圖的資料(電話號碼)startActivity(intent);// 使用意圖開啟一個介面(打電話的介面)}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}


2、main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >        <TextView         android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/a"        />        <EditText         android:id="@+id/num_et"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:hint="@string/c"        />        <Button         android:id="@+id/call_bt"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/b"        android:onClick="hello"        /></LinearLayout>


源碼下載:http://download.csdn.net/detail/caihongshijie6/7600651








聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.