(Android basic knowledge review) Call
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 ();/*** The following is the core code for calling: */Intent intent = new Intent (); // create the intent object Intent. setAction (Intent. ACTION_CALL); // specifies the intent action (CALL) intent. setData (Uri. parse ("tel:" + phoneNum); // set the 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 ;}}
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"> enter the phone number </string> <string name = "B"> call number </string> <string name = "c ""> enter the number here </string> </resources>
4. AndroidManifest. xml
In this configuration file, add:
<uses-permission android:name="android.permission.CALL_PHONE"/>
--------------------------- The first method to handle click events when used above. The following describes the second and third processing methods for event processing ------------
Method 2:
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 ();/*** The following is the core code for calling: */Intent intent = new Intent (); // create the intent object Intent. setAction (Intent. ACTION_CALL); // specifies the intent action (CALL) intent. setData (Uri. parse ("tel:" + phoneNum); // set the intent data (phone number) startActivity (intent); // use the 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 ;}}
Other code does not need to be changed
Method 3:
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 ();/*** The following is the core code for calling: */Intent intent = new Intent (); // create the intent object Intent. setAction (Intent. ACTION_CALL); // specifies the intent action (CALL) intent. setData (Uri. parse ("tel:" + phoneNum); // set the intent data (phone number) startActivity (intent); // use the 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: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>
Source code download: http://download.csdn.net/detail/caihongshijie6/7600651