Package com. example. test; import android.net. uri; import android. OS. bundle; import android. app. activity; import android. content. intent; import android. text. textUtils; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. editText; import android. widget. textView; import android. widget. toast; public class MainActivity extends Activity implements OnClickListener {Button bt_call; private EditText TV _phonenumber; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // load the layout file setContentView (R. layout. activity_main); // Search Button bt_call = (Button) findViewById (R. id. call); // search for the text box TV _phonenumber = (EditText) MainActivity. this. findViewById (R. id. et_phonenumber); // set Click Event 1 for the button. create an internal class definition Click Event // bt_call.setOnClickListener (new mylisnter ();/* bt_call.setOnClickListener (new OnClickListener () {// 2. use an anonymous internal class to create a click event @ Overridepublic void onClick (View v) {callphone () ;}}); * // 3. let the activity implement the Click Event interface // bt_call.setOnClickListener (this);} // 4. bind a click method public void callButtonClicked (View view) {callphone ();} private class mylisnter implements OnClickListener {/** to the layout file. When the button is clicked, call method **/@ Overridepublic void onClick (View v) {callphone () ;}} private void callphone () {String number = TV _phonenumber.getText (). toString (). trim (); if (TextUtils. isEmpty (number) {Toast. makeText (MainActivity. this, "Enter the phone number", Toast. LENGTH_SHORT ). show (); return;} // intention. What do you want to do? Intent intent = new Intent (); intent. setAction (intent. ACTION_CALL); intent. setData (Uri. parse ("tel:" + number); startActivity (intent);} // method not implemented in the interface @ Overridepublic void onClick (View v) {switch (v. getId () {case R. id. call: callphone (); break ;}}}
Layout
Android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: feature = "true" android: layout_below = "@ id/et_phonenumber" android: layout_marginTop = "16dp" android: text = "@ string/bt_call"/>