View. javasmclick () triggers the click event, and then click
1. Main Functions
Automatically trigger the Click Event of the control
2. layout file activity_main.xml
<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"> <Button android: id = "@ + id/bt" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Click here"/> <Button android: id = "@ + id/bt2" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_below = "@ id/bt" android: text = "@ string/hello_world"/> </RelativeLayout>
3. MainActivity code
Package com. android; import android. app. activity; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. toast; public class MainActivity extends Activity {Button button1; Button button2; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); button1 = (Button) findViewById (R. id. bt); button1.setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {button2.20.mclick (); // call the button2 Click Event}); button2 = (Button) findViewById (R. id. bt2); button2.setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {Toast. makeText (MainActivity. this, "click", Toast. LENGTH_SHORT ). show ();}});}}
4. Notes
If the view. setOnTouchListener () method is used at the same time, the response event of view. javasmclick () may be intercepted,
Because when view. OnTouchEvent () is in event. getAction () = MotionEvent. ACTION_DOWN, false is returned,
The system will think that the view does not need to process Touch events, and subsequent Touch events (move, up, and click) will not be passed in.
So it will not trigger view. javasmclick (), while view. setOnTouchListener () is equivalent to overwriting view. OnTouchEvent (),
Therefore, when writing the TouchListener processing of the view, you need to pay attention to whether the view has a click event listener. If so, use view. javasmclick () to trigger the click event in the appropriate position.