【发布时间】:2019-12-04 05:00:29
【问题描述】:
一个按钮将字符串添加到 2 个 EditTexts 中,并在 TextView 中显示结果。然后第二个按钮应该将这些字符串与两个硬编码的单独单词进行比较。 “你好世界”。我什至无法将字符串连接起来并在 Textview 中显示输出。 这是我的代码
public class MainActivity extends AppCompatActivity {
//The stings to hold the user input
EditText str1;
EditText str2;
//string to hold the output
TextView display1;
EditText enter1;
EditText enter2;
TextView WDisplay;
Button add;
Button display;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
str1 = (EditText)findViewById(R.id.enter1);
str2 = (EditText)findViewById(R.id.enter2);
add = (Button) findViewById(R.id.add);
display = (Button) findViewById(R.id.display);
display1 = (TextView)findViewById(R.id.WDisplay);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String str1 = enter1.getText().toString();
String str2 = enter2.getText().toString();
display1.setText(str1);
}
});
display.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String str1 = enter1.getText().toString();
String str2 = enter2.getText().toString();
display1.setText(str1);
/*String check1;
{
check1 = "Hello";
}
String check2;
{
check2 = "World";
}*/
}
});
}'
我似乎无法将基本内容与我的程序所需的内容联系起来。请帮忙!
【问题讨论】:
-
当你想连接你必须使用行 display1.setText(str1); 中的一些方法- 目前这只会显示 str1
-
从我正在做的大量研究中收集到的信息,我发现了很多使用 display1.setText(str1); 格式的示例。我尝试使用“”,但没有用。
-
为您搜索的内容添加了答案。在编码时尝试遵循最佳实践。在这里为您提供准确的答案并不是一个好主意。附加的链接将在这里为您提供帮助。
标签: java android android-edittext concat