【问题标题】:How to know which button is clicked in android?如何知道在android中单击了哪个按钮?
【发布时间】:2017-06-04 18:49:43
【问题描述】:

我需要知道点击了哪个动态生成的按钮。我的目标是了解用户点击的按钮并根据点击的按钮使用信息。我把代码放在这里,我特别需要 click() 方法的帮助。有人可以帮帮我吗?

提前致谢!

public class B3CalibrationExisting extends AppCompatActivity  {

    Button[]  oldButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.b3calibration_existing);
        getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        getSupportActionBar().setCustomView(R.layout.a0action_bar);

        Intent intent = getIntent();

        LinearLayout layout = (LinearLayout) findViewById(R.id.activity_existingcalibration);
        layout.setOrientation(LinearLayout.VERTICAL);

        ScrollView scrollView = new ScrollView(this);
        layout.addView(scrollView);

        LinearLayout row = new LinearLayout(this);
        row.setOrientation(LinearLayout.VERTICAL);
        row.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        scrollView.addView(row);

        SharedPreferences sharedPref = this.getSharedPreferences("PREFERENCE_FILE_KEY", Context.MODE_PRIVATE);
        int t = sharedPref.getInt("times",0);

        Set<String> set = sharedPref.getStringSet("calibrationset",null);
        String[] Name = set.toArray(new String[t]);//new String[t]

        String calName = "";
        for(int i=t; i>=0; i--) {

            oldButton[i] = new Button(this);
            oldButton[i].setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
            calName = sharedPref.getString("Button_"+i,"");
            oldButton[i].setText(calName);
            oldButton[i].setId(i);
            row.addView(oldButton[i]);

            oldButton[i].setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    //your desired functionality
                    click();
                }
            });

        }
    }

    public void click(){

      //I NEED HELP FOR THIS PART 
      // IF I CAN GET ID OF CLICKED BUTTON I WILL BE ABLE TO DO WHAT I WANT 

        int id = oldButton[].getId();

        SharedPreferences sharedPref = this.getSharedPreferences("PREFERENCE_FILE_KEY", Context.MODE_PRIVATE);

        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putInt("id",id);

        editor.commit();

        Intent intent2 = new Intent(this, A3ExperimentFirst.class);
        startActivity(intent2);
    }
}

【问题讨论】:

标签: android button dynamic onclick click


【解决方案1】:

这可能会奏效:

public void onClick(View v){
  click(v);
}

public void click(View v){
  int id = v.getId();
  // and so on
}

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    我建议你修改一下你的代码:

    final int id=i;
    oldButton[i].setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                click(id);
            }
        });
    

    然后重做你的点击函数直接获取ID:

    public void click(int id){
    
    /* there is no more need for this line since id is passed as parameter
    int id = oldButton[].getId();
    */
    
        SharedPreferences sharedPref = this.getSharedPreferences("PREFERENCE_FILE_KEY", Context.MODE_PRIVATE);
    
    
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putInt("id",id);
    
    
        editor.commit();
    
    
        Intent intent2 = new Intent(this, A3ExperimentFirst.class);
        startActivity(intent2);
    

    }

    但老实说,我认为这是一种不好的处理方式。您最好直接在点击侦听器上编写“click()”代码,并使用作为参数传递的“view”来识别按钮。您可以使用列表代替 Button 序列,并使用 get(view) 获取 ID。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-17
      • 2021-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多