【发布时间】: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);
}
}
【问题讨论】:
-
请在 (stackoverflow.com/questions/9192109/…) 中查看答案,它可能会有所帮助
标签: android button dynamic onclick click