在表格布局中,每行几乎没有 TextView 和按钮。现在,当我单击特定按钮时,只有与该特定按钮相关的 TextView 值必须传递到下一个 Intent 。我怎样才能实现这一点?请询问您是否需要任何进一步的信息。
TableLayout tl = (TableLayout) findViewById(R.id.tableLayout1);
for(i=0;i<count;i++)
{
// Create a TableRow and give it an ID
TableRow tr = new TableRow(this);
// Create a TextView to house the name of the province
TextView labelTV = new TextView(this);
labelTV.setText(smsListDate.get(i));
labelTV.setTextColor(Color.WHITE);
// Create a TextView to house the value of the after-tax income
TextView valueTV = new TextView(this);
valueTV.setText(smsListMessage.get(i));
tr.addView(valueTV);
TextView valueTV2 = new TextView(this);
valueTV.setTextColor(Color.WHITE);
Button submit = new Button(this);
submit.setText("respond");
tr.addView(submit);
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
谢谢
我认为理解上存在一些差距。我确实将提交操作写为
submit.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(getApplicationContext(),
SmsActivity.class);
startActivity(myIntent);
}});
但我现在的目的是将特定行中 TextView 的值传递到单击该特定按钮的下一个 Intent 。
最佳答案
您将需要一个用于提交按钮的按钮监听器,并且在其 onClick 方法中,您需要将值放入可以传递到下一个 Intent 的 bundle 中。
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//create an intent
Intent intent = new Intent(ClassYouAreIn.this,
ClassForNextIntent.class);
//put the text view value into the bundle for the intent
//the bundle is a Map and uses key/value pairs
//(it is like a dictionary)
//in this case the key is myTextViewValue
//and the value is valueTV.getText()
intent.putExtra("myTextViewValue", valueTV.getText());
//now start your new activity with the intent you just made
startActivity(intent);
}
});
现在,在您的其他类(新 Intent 将转到的类)中,您将需要访问该包并获取您想要传递给该 Intent 的值。对于此示例,我将在 onCreate 方法中获取 bundle 数据,但您实际上可以使用任何您想要的方法从 bundle 中获取数据。
@Override
public void onCreate(Bundle bundle)
{
super.onCreate(bundle);
setContentView(R.layout.your_view);
//get the extra(s) you put in the bundle
bundle = getIntent().getExtras();
String valueFromTV = bundle.getString("myTextViewValue");
//and then do whatever you want with it
}
希望对你有帮助
关于java - 处理android中的多个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6454911/