Android learning-four types of listening events, android listening
You are welcome to reprint it in any form, but be sure to indicate the source.
Reference: http://blog.csdn.net/a78270528/article/details/46953541
XML file
The focus is to set the id for the two controls. In java code, we can find the corresponding control through the id to set the listening event for it.
<TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="Hello Liu!" android:textSize="40dp" android:textStyle="bold" android:layout_marginTop="20dp" /><Button android:text="Button" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/textView" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:id="@+id/bt_dial" />
I. Anonymous internal class
This method is more intuitive and convenient for writing, but it will become huge when the button is added.
Public class MainActivity extends AppCompatActivity {// use the id to find the control private Button bt_dial = (Button) findViewById (R. id. bt_dial); private TextView textview = (TextView) findViewById (R. id. textView); @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // load the layout bt_dial.setOnClickListener (new View. onClickListener () {// set the listener event @ Override public void onClick (View view) {textview. setText ("Hello World"); // click the button to change the text in textview2 }});}}
Ii. Custom click event listening class
Public class MainActivity extends AppCompatActivity {// use the id to find the control private Button bt_dial = (Button) findViewById (R. id. bt_dial); private TextView textview = (TextView) findViewById (R. id. textView); @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // load layout bt_dial.setOnClickListener (new btListener (); // trigger event} private class btListener implements View. onClickListener {// custom listener class, inheriting OnClickListener public void onClick (View view) {// implementation method textview. setText ("Hello World"); // click the button to change the content of the text box }}}
3. The Activity inherits View. OnClickListener.
The OnClick (View view) method is implemented by Activity. In The OnClick (View view) method, switch-case is used to process the buttons represented by different IDs.
This method is the best way to use Onclick.
Public class MainActivity extends AppCompatActivity implements View. onClickListener {// use the id to find the control private Button bt_dial = (Button) findViewById (R. id. bt_dial); private TextView textview = (TextView) findViewById (R. id. textView); @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // load the layout bt_dial.setOnClickListener (this); // trigger event} public void onClick (View view) {// implementation method switch (view. getId () {case R. id. bt_dial: textview. setText ("Hello World"); break; default: break ;}}}
4. Display The onClick attribute of the specified button in the XML file
In this way, the click () method in the corresponding Activity is called Using Reflection when you click the button:
In this way, the Click Event of the button can be implemented without declaring the button in the code.
android:onClick="setTexview"
SetTexview is a method name. You need to implement this method in the Activity corresponding to the layout.
public void setTexview(View view){ textview.setText("Hello World"); }