![](/img/trans.png)
[英]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.