One, in XML
< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<!--current order->
xmlns:tools="Http://schemas.android.com/tools"
android:layout_width="Match_parent" <!--full Screen overlay->
android:layout_height="Match_parent"
android:orientation="vertical" <!--vertical Distribution->
tools:context="${relativepackage}.${activityclass}">
< EditText
android:id="@+id/edit_text" <!--to get EditText-->
android:layout_width="Wrap_content" <!--Package Contents-->
android:layout_height="Wrap_content"
android:text="@string/hello_world1"/><!--Strings.xml introduced-->< C12>>
<Button
android:id="@+id/clear_button"
android:layout_width="Wrap_content"
android:layout_height="Wrap_content"
android:text="Click to clear Input"/>
</ LinearLayout>
//---------------------------------------------------------
In the corresponding. java
Package com.example.myandroid;
import android.app.Activity;
import Android.os.Bundle;
Import Android.view.Menu;
Import Android.view.MenuItem;
import Android.view.View;
Import Android.view.View.OnClickListener;
import Android.widget.Button;
import Android.widget.EditText;
Public class Mainactivityextends Activity {
@Override
protected void onCreate (Bundle savedinstancestate) {
Super. OnCreate (savedinstancestate);
Setcontentview (r.layout. Activity_main);
Final EditText EditText = (EditText) Findviewbyid (r.id. Edit_text);//Get control
Button button = (button) Findviewbyid (r.id. Clear_button);
Button.setonclicklistener (new Button.onclicklistener () {//Create listener
Public void OnClick (View v) {
Edittext.settext ("");
}
});
};
}
There is no change in XML in. java
Button.setonclicklistener (listener);
Button.onclicklistener Listener = new Button.onclicklistener () {//Create Listener Object
Public void OnClick (View v) {
editText. SetText ("");
}
};
Third, the button revision in the XML is changed to
<button
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/button_send"
android:onclick= "SendMessage"/>
The. java file
public void SendMessage (view view) {
editText. SetText ("");
}
Android button Click event Three ways