[英]Android java - correct way to handle multiple buttons from main activity
[英]handle multiple buttons in android
在表格布局中,每行都有很少的文本视图和按钮。 现在,当我单击某个特定按钮时,仅必须将与该特定按钮相关的文本视图值传递给下一个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);
}});
但是我现在的意图是将特定行中的文本视图的值传递给下一次单击该特定按钮的意图。
您将需要一个按钮侦听器作为您的提交按钮,并需要在其onClick方法中将值放入可传递给下一个意图的包中。
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);
}
});
现在在您的另一个类(新意图将进入的类)中,您将需要访问包并获取您想要传递给该意图的值。 对于此示例,我将在我的onCreate方法中获取捆绑包数据,但您实际上可以通过任何所需的方法从捆绑包中获取数据。
@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
}
希望这对您有帮助
您需要为按钮创建一个点击侦听器,然后在其中创建意图并放置一个字符串,您可以从valueTV文本中获取该字符串。
submit.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent i = new Intent();
i.putExtra("value", valueTV.getText());
startActivity(i);
}
});
嗯...您的问题对我来说还不是很清楚,但是我认为对于初学者来说,您需要添加一个OnClickListener(我使用匿名用户)“提交”,然后在其中创建/启动您的意图onClick(View)方法。 您可以使用Intent.putExtra(String,String)传递值。 像这样:
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(ThisActivity.this, NextActivity.class);
intent.putExtra("value", valueTV.getText())
startActivity(intent);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.