The main is to call this button's corresponding code to write a method, by calling this method to make the code look more concise neat:
The code for Mainactivity.java is as follows:
PackageCom.example.dailcall;Importandroid.support.v7.app.ActionBarActivity;Importandroid.text.TextUtils;Importandroid.content.Intent;ImportAndroid.net.Uri;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;Importandroid.view.view;//to import this package name when creating a Onclicklistener interfaceImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.EditText;ImportAndroid.widget.Toast; Public classMainactivityextendsactionbaractivity {//Define a Build PrivateEditText et; PrivateButton btn; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); ET= (EditText) Findviewbyid (R.id.et_number);//use Findviewbyid to find the build name and bind to the defined build variable. btn=(Button) Findviewbyid (R.id.btn_dail); Btn.setonclicklistener (NewMyclicklistener ()); } Private classMyclicklistenerImplementsonclicklistener{@Override Public voidOnClick (View v) {//TODO auto-generated Method StubCallnumber (); }} @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; } @Override Public Booleanonoptionsitemselected (MenuItem item) {//Handle Action Bar item clicks here. The Action Bar would//automatically handle clicks on the Home/up button, so long//As you specify a the parent activity in Androidmanifest.xml. intID =Item.getitemid (); if(id = =r.id.action_settings) { return true; } return Super. onoptionsitemselected (item); }//Callnumber () This action as a private methodPrivate voidCallnumber () {Intent Intent=NewIntent (); String Number=Et.gettext (). toString (). Trim (); if(Textutils.isempty (number)) {Toast.maketext (mainactivity. This, "The phone number cannot be empty!" ", 0). Show (); return; } intent.setaction (Intent.action_call); Intent.setdata (Uri.parse ("Tel:" +Number )); StartActivity (Intent); }}
The code for Activity_main.xml is as follows:
<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:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context= "Com.example.dailcall.MainActivity" > <EditText Android:id= "@+id/et_number"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:inputtype= "Phone" > <requestfocus/> </EditText> <Button Android:id= "@+id/btn_dail"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_below= "@+id/et_number"Android:text= "@string/dail"/></relativelayout>
Android Learning Note 002-call (Optimize)