我需要知道点击了哪个动态生成的按钮。我的目标是知道用户点击的按钮并根据点击的按钮使用信息。我把代码放在这里,我特别需要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);
}
}
答案 0 :(得分:0)
这可能会起到作用:
library(plotly)
dt <- data.frame(noiseLevel=sort(rep(seq(10,50,10),5)),
temperature=rep(seq(50,90,10),5),
testScore=c(30,75,20,50,70,
40,60,45,20,50,
30,20,30,40,30,
60,30,60,20,50,
30,50,20,40,60))
dt <- within(dt, {
csum <- ave(testScore, noiseLevel, FUN=cumsum)})
plot_ly(data=dt,
x = ~noiseLevel,
y = ~testScore,
color=~factor(temperature),
type='bar') %>%
layout(barmode='stack',
annotations = list(x=~noiseLevel, y=~csum, text=~testScore,
xanchor = 'center', yanchor = 'center',
showarrow = FALSE))
希望这有帮助!
答案 1 :(得分:0)
我建议你修改一下你的代码:
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);
}
然而,尽管如此,我认为这是一个糟糕的交易方式。你最好直接写下你的&#34; click()&#34;点击监听器上的代码,并使用&#34;视图&#34;作为参数传递以识别按钮。您可以使用列表而不是Button序列,并使用get(view)来获取ID。