Best way to implement View.OnClickListener in android

后端 未结 14 2453
盖世英雄少女心
盖世英雄少女心 2020-11-27 05:25

Suppose we have an Activity with a lot of views on which OnClickListener is to be registered.

The most common way to implement this is to let the Activi

14条回答
  •  北荒
    北荒 (楼主)
    2020-11-27 06:13

     public class ProfileDetail extends AppCompatActivity implements View.OnClickListener {
    
              TextView tv_address, tv_plan;
    
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_profile_detail);
    
    
    
                tv_address = findViewById(R.id.tv_address);
                tv_plan = findViewById(R.id.tv_plan);
    
                tv_address.setOnClickListener(this);
                tv_plan.setOnClickListener(this);
        }
     @Override
        public void onClick(View view) {
                switch (view.getId()) {
                    case R.id.tv_plan:
                        startActivity(new Intent(getApplicationContext(),PlanActivity.class));
                        break;
                    case R.id.tv_address:
                         startActivity(new Intent(getApplicationContext(),AddressActivity.class));
                        break;
                }
            }
    }
    

提交回复
热议问题