我正在尝试收集用户输入并将其传递给另一个活动并使用文本视图显示它。
我已经尝试使用getText()和toString()函数并传递intent,但是在运行程序时,应该包含用户输入的字符串无法正确显示。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_in);
//Collect user inputs and store them in strings
noun1 = (EditText)findViewById(R.id.noun1);
pluralNoun = (EditText)findViewById(R.id.pluralNoun);
noun2 = (EditText)findViewById(R.id.noun2);
place = (EditText)findViewById(R.id.place);
adjective = (EditText)findViewById(R.id.adjective);
noun3 = (EditText)findViewById(R.id.noun3);
firstNoun = noun1.getText().toString();
nounPlural = pluralNoun.getText().toString();
secondNoun = noun2.getText().toString();
inputPlace = place.getText().toString();
inputAdjective = adjective.getText().toString();
thirdNoun = noun3.getText().toString();
madLib ="Be kind to your " + firstNoun + "-footed " + pluralNoun + "\n" +
"For a duck may be somebody`s " + secondNoun + ",\n" +
"Be kind to your " + pluralNoun + " in " + inputPlace + "\n" +
"Where the weather is always " + inputAdjective + ".\n" +
"\n" +
"You may think that this is the " + thirdNoun + ",\n" +
"Well it is.\t";
}
public void createStory(View view) {
Intent myIntent = new Intent(MainActivityIn.this,
MainActivityOut.class);
myIntent.putExtra("story",madLib);
startActivity(myIntent);
}
我希望输出几个句子,用用户输入的单词显示故事,但我得到如下图所示的输出:
您可以使用下面的代码在另一个名为MainActivityOut.class的活动中检索String,并将该字符串传递给文本视图。
String madLib = getIntent().getStringExtra("story");
yourTextView.setText(madLib);
在matLib中,你调用的一些变量不是String而是editText,这就是为什么你有这些奇怪的结果。喜欢pluralNoun ...尝试这个命名约定,命名所有editText et ...,所有textView tv ...,所有按钮btn ...,所有imageView iv ...这样你可以避免混淆你的变量。
你在字符串中传递EditText
对象
madLib ="Be kind to your " + firstNoun + "-footed " + pluralNoun + "\n" +
"For a duck may be somebody`s " + secondNoun + ",\n" +
"Be kind to your " + pluralNoun + " in " + inputPlace + "\n" +
"Where the weather is always " + inputAdjective + ".\n" +
"\n" +
"You may think that this is the " + thirdNoun + ",\n" +
"Well it is.\t";
复数名词是qazxsw poi对象
改变代码就好
EditText