The Click event of button in Android is very simple, mainly an internal class problem
There are two buttons and a text box on the interface, and when you click a different button, the text message of the different buttons is displayed in the box.
<?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" > <EditText Android:id= "@+id/txtpassword"Android:text=""Android:layout_width= "Fill_parent"Android:layout_height= "70px"/> <Button Android:id= "@+id/btnfirst"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "First"/> <Button Android:id= "@+id/btnsecond"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Second"/> </LinearLayout>
Layout Code XML content
Using the above code can see the interface shown, as to how to handle the triggering event, please refer to the following code
PackageCom.git.ch3;ImportAndroid.os.Bundle;Importandroid.app.Activity;ImportAndroid.app.AlertDialog;ImportAndroid.content.Context;ImportAndroid.content.DialogInterface;Importandroid.text.util.Linkify;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.view.ViewGroup;ImportAndroid.widget.BaseAdapter;ImportAndroid.widget.Button;ImportAndroid.widget.EditText;ImportAndroid.widget.Gallery;ImportAndroid.widget.ImageView;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {PrivateButton btnfirst=NULL; PrivateButton btnsecond=NULL; PrivateEditText lbltitle=NULL; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Btnfirst=(Button) Findviewbyid (R.id.btnfirst); Btnsecond=(Button) Findviewbyid (R.id.btnsecond); Lbltitle=(EditText) Findviewbyid (R.id.txtpassword); Btnfirst.setonclicklistener (NewButton.onclicklistener () {@Override Public voidOnClick (View v) {String First=Btnfirst.gettext (). toString (); Lbltitle.settext (first); } }); Btnsecond.setonclicklistener (NewButtonClick ()); } Private classButtonClickImplementsonclicklistener{@Override Public voidOnClick (View v) {String First=Btnsecond.gettext (). toString (); Lbltitle.settext (first); } }}button click event
Two buttons trigger different events, but there is a difference between the two, which is used in the inner class of Java
Android Button click event