我正在尝试收集用户输入并将其传递给另一个活动,并使用文本视图显示它。
我尝试使用getText()和toString()函数并传递意图,但是在运行程序时,应包含用户输入的字符串无法正确显示。
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);
}
我希望输出一些句子来显示带有用户输入的单词的故事,但我得到的输出如下图所示:
答案 0 :(得分:0)
您可以使用以下代码在另一个名为 MainActivityOut.class 的活动中检索您的字符串,并将该字符串传递到文本视图。
String madLib = getIntent().getStringExtra("story");
yourTextView.setText(madLib);
答案 1 :(得分:0)
在matLib中,您调用的某些变量不是String而是editText,这就是为什么您得到那些奇怪结果的原因。就像pluralNoun ...尝试使用这种命名约定,将所有editText等名称,将所有textView电视名称,将所有按钮btn ...,所有imageView iv ...这样可以避免混淆变量。>
答案 2 :(得分:0)
您正在传递 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";
pluralNoun是 EditText
对象
更改代码,例如
madLib ="Be kind to your " + firstNoun + "-footed " + nounPlural+ "\n" +
"For a duck may be somebody`s " + secondNoun + ",\n" +
"Be kind to your " + nounPlural + " in " + inputPlace + "\n" +
"Where the weather is always " + inputAdjective + ".\n" +
"\n" +
"You may think that this is the " + thirdNoun + ",\n" +
"Well it is.\t";