Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
327 views
in Technique[技术] by (71.8m points)

java - How to check a button was clicked without overried onclick method?

This is my MainActivity, button and onclick function:

public class MainActivity extends AppCompatActivity {
    EditText etName;
    Button btSubmit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        etName = findViewById(R.id.et_name);
        btSubmit = findViewById(R.id.bt_submit);
        AwesomeValidation awesomeValidation = new AwesomeValidation(ValidationStyle.BASIC);
        awesomeValidation.addValidation(this, R.id.et_name, RegexTemplate.NOT_EMPTY, R.string.invalid_name);

        btSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openSubActivityEmty();
                if (awesomeValidation.validate()) {
                    //On success
                    Toast.makeText(getApplicationContext(), "Form Validate Succefull..", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getApplicationContext(), "Submit faild!", Toast.LENGTH_SHORT).show();
                }
            }
        });

Now i'm trying write a new class which use to find the button and check if the button was click one more time?

So can i do this or someone has any solution for this task?

question from:https://stackoverflow.com/questions/65626402/how-to-check-a-button-was-clicked-without-overried-onclick-method

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's not possible to track a click without overriding the onClick method. If you want to check if the button is clicked you can just add a boolean variable isClicked and make it false by default. Inside the onClick, make it true. If you want to access it from a different class, make it public.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...