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:o rientation= "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 ();/** * The following is the core code of the call: */intent Intent = new Intent ();//Create Intent Object Intent.setaction (Intent.action_call) ///Set Intent Action (call) Intent.setdata (Uri.parse ("Tel:" + phonenum))///Set Intent data (phone number) startactivity (intent);//Use intent to open an interface ( The Calling Field)}} @Overridepublic Boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it I S 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" > Please enter phone number </string> <string name= "B" > Call number </string> <string name= "C" > enter your number here </string></resources>
4, Androidmanifest.xml
In this configuration file, add:
<uses-permission android:name= "Android.permission.CALL_PHONE"/>
---------------------------The first thing to do when you click on the event's handling. The second and third way to handle event processing is described below------------
The second method of event handling:
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 ();/** * Following is the core code of the call: */intent Intent = new Intent ();//Create Intent Object Intent.setaction (Intent.action_call);//Set Intent Action (call) Intent.setdata (Uri.parse ("Tel:" + phonenum);// Set Intent data (phone number) startactivity (intent);//Use intent to open an interface (called Interface)} @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); re Turn true;}}
No other code needs to change.
The third type of event handling:
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 ();/** * Following is the core code of the call: */intent Intent = new Intent () ;//Create Intent Object Intent.setaction (Intent.action_call);//Set Intent Action (call) Intent.setdata (Uri.parse ("Tel:" + phonenum);// Set Intent data (phone number) startactivity (intent);//Use intent to open an interface (call interface)} @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:o rientation= "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>
SOURCE Download: http://download.csdn.net/detail/caihongshijie6/7600651