我正在做一个包含电话联系列表视图和复选框的应用程序。我找到了适合我项目的编码。它运作良好但它不能按我的意愿运行。我不希望'respondText'出现在toast功能中,我只是希望它在点击按钮后显示在另一个新活动中。我尝试了intent函数,我把它放在toast funstion下但是应用程序没有执行。也许我只是放错了功能,处理这个问题的实际解决方案是什么?这是它:
private void checkButtonClick() {
Button myButton = (Button) findViewById(R.id.findSelected);
final TextView textView = (TextView) findViewById(R.id.showAppend);
myButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
StringBuffer responseText = new StringBuffer();
responseText.append("The following were selected...\n");
ArrayList<Contacts> countryList = dataAdapter.contactList;
for (int i = 0; i < countryList.size(); i++) {
Contacts country = countryList.get(i);
if (country.isSelected()) {
responseText.append("\n" + country.getName());
}
}
//Toast.makeText(getApplicationContext(), responseText,
//Toast.LENGTH_LONG).show();
Intent startNewAct = new Intent(ListViewCheckboxesActivity.thisthis, DisplayAppendActivity.class);
startActivity(startNewAct);
textView.setText(responseText);
}
});
}
这是另一项新活动:
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class DisplayAppendActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.displayappend);
TextView textView = (TextView) findViewById(R.id.showAppend);
String responseText = getIntent().getExtras().getString("responseText");
textView.setText(responseText);
}
}
答案 0 :(得分:1)
您可以通过Intent发送数据。从意图开始动作时,您可以使用:
Intent startNewAct = new Intent(ListViewCheckboxesActivity.this, DisplayAppendActivity.class);
startNewAct.putExtra("responseText", responseText);
startActivity(startNewAct);
你可以在另一个活动onCreate上获得这个responseText:
String responseText = getIntent().getExtras().getString("responseText");