View. javasmclick () triggers the click event, and then click

Source: Internet
Author: User

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.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.