Write the Android program for the first time and write a simple dialer: very simple
As follows:
The code for Mainactivity.java is as follows:
PackageCom.example.dailcall;Importandroid.support.v7.app.ActionBarActivity;Importandroid.content.Intent;ImportAndroid.net.Uri;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.EditText; Public classMainactivityextendsactionbaractivity {//define buildPrivateEditText et; PrivateButton btn; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); ET=(EditText) Findviewbyid (r.id.et_number);//through Findviewbyid to find the build name, bind to the defined build variable. BTN=(Button) Findviewbyid (R.id.btn_dail); Btn.setonclicklistener (NewView.onclicklistener () {//define a Click event response for the button @Override Public voidOnClick (View v) {//TODO auto-generated Method StubIntent intent=NewIntent ();//define a Intent. Intent.setaction (Intent.action_call); Intent.setdata (Uri.parse ("Tel:" +Et.gettext (). toString (). Trim ())); /WHERE Tel: Be sure to add, otherwise error startactivity (intent); } }); } @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); }}
The Activity_main.xml layout file 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"//the ID of the definition must be meaningful, convenient for yourself and others to read and quote.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 001-making a phone call