Android call onClick method without Clicking

前端 未结 4 834
野趣味
野趣味 2021-01-11 21:49

I want to use existing onClick method to make my program simpler. It consists of onClick method and other method:

@Override
public void onClick(View v) {
  s         


        
4条回答
  •  囚心锁ツ
    2021-01-11 22:52

    public class DemoActivity extends AppCompatActivity implements View.OnClickListener{         
    
    Button mBtnAutomaticClick;
            protected void onCreate(Bundle savedInstanceState) {
                 super.onCreate(savedInstanceState);
                 setContentView(R.layout.activity_report_bug);
                 mBtnAutomaticClick = findViewById(R.id.automatic_click_demo);
                 mBtnAutomaticClick.setOnClickListener(this);
                 mBtnAutomaticClick.performClick(); // for automatic click event
              }
          }
    
    @Override
    public void onClick(View v) {
         switch (v.getId()) {
            case R.id.automatic_click_demo:
               // your code
               break;
        }
    }
    

    When activity will call your code which you have written inside onClick will be called automatically.

提交回复
热议问题