【问题标题】:How to handle click event in recyclerview in Android?如何在Android的recyclerview中处理点击事件?
【发布时间】:2019-01-14 19:36:06
【问题描述】:

我将单选按钮放在回收站视图中。当我单击一个单选按钮导航到具有选定值的其他页面时。

【问题讨论】:

  • 添加你的测试代码

标签: android list radio-button android-recyclerview


【解决方案1】:
 @Override
    public void onBindViewHolder(ViewHolder viewHolder, final int position) {

    viewHolder.radioButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               // handle click event here
            }
        });

        viewHolder.itemView.setTag(your_item);

    }

【讨论】:

    【解决方案2】:

    使用:
    在 onBind 中,只需将您的数据设置为标签

        radioButton.setTag(yourData);
    
        radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    yourData = buttonView.getTag();
                    //Do code here
                }
            });
    

    【讨论】:

      【解决方案3】:

      如果您使用的是 Radio 组,那么您必须执行这样的代码

                Button bt = (Button) findViewById(R.id.btnDisplay);
      
                bt.setOnClickListener(new OnClickListener() {
      
                @Override
               public void onClick(View v) {
      
              // get selected radio button from radioGroup
              int selectedId = radiogroup .getCheckedRadioButtonId();
      
              // find the radio button by returned id
              RadioButton radioButton = (RadioButton) findViewById(selectedId);
      
                  Toast.makeText(MainActivity.this, 
                 radioButton.getText(),Toast.LENGTH_SHORT).show();
      
                  Intent intent = new Intent(this,nextActivity.class);
      
                  Then put the information you want to pass on the intent:
      
                  intent.putExtra("checked",selectedId );
      
                  Then start the next activity:
      
                  startActivity(intent);
      
                  On the next activity you recover that info like this:
      
                  Intent intent = getIntent();
      
                  int checked = intent.getIntExtra("checked");
      
          }
         });
      

      如果你只使用单选按钮,那么你必须做这样的代码

           public class MainActivity extends AppCompatActivity {
      
      RadioButton android;
      
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
      
          setContentView(R.layout.activity_main);
      
          android = (RadioButton)findViewById(R.id.rdbAndroid);
      
      }
      public void onRadioButtonClicked(View view) {
          boolean checked = ((RadioButton) view).isChecked();
      
          String str="";
      
          // Check which radio button was clicked
      
          switch(view.getId()) {
      
              case R.id.rdbAndroid:
      
                  if(checked)
      
                    str = "Android Selected";
      
                    android.setchecked(true); 
      
                     Intent intent = new Intent(this,nextActivity.class);
      
                  Then put the information you want to pass on the intent:
      
                  intent.putExtra("checked",str );
      
                  Then start the next activity:
      
                  startActivity(intent);
      
                  On the next activity you recover that info like this:
      
                  Intent intent = getIntent();
      
                  int checked = intent.getIntExtra("checked");
      
      
                  break;
      
          }
      
          Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();
      }
      

      }

      【讨论】:

        【解决方案4】:

        在 Recyclerview 中你必须这样做

             // declare variable :
        
             String typeId,fueltype,droidId,droidType ;
        
             Radiobutton droid,android  ;
        
            // get the id of the view 
        
            android = (RadioButton)findViewById(R.id.rdbAndroid);
        
           droid = (RadioButton)findViewById(R.id.rdbdroid );
        
          //Click listner 
        
         android .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        
         @Override
         public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        
         if (android .isChecked()) {
        
        fuelType = petrol.getText().toString();
        
        typeId="1";
        
         Intent intent = new Intent(this,nextActivity.class);
        
                Then put the information you want to pass on the intent:
        
                intent.putExtra("checked",typeId);
        
                Then start the next activity:
        
                startActivity(intent);
        
                On the next activity you recover that info like this:
        
                Intent intent = getIntent();
        
                int checked = intent.getIntExtra("checked");
        
                }else if(droid.ischecked()){
                droidType = droid.getText().toString();
        
               droidId="2";
        
               Then put the information you want to pass on the intent:
        
                intent.putExtra("checked",droidId);
        
                Then start the next activity:
        
                startActivity(intent);
        
                On the next activity you recover that info like this:
        
                Intent intent = getIntent();
        
                int checked = intent.getIntExtra("checked");
        
        
               // or you can use tag
        
               android.setTag(yourData);
        
               droid.setTag(yourData);
        
               yourData = compoundButton.getTag();
               }
        
        
        
                }
                }
                 });
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-06-08
          • 2013-07-24
          • 2014-08-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多