我想在EditText
中显示7 Toast
的信息,因此我将编辑文本信息放在7个字符串变量中,但我不知道如何。
这是我写的:
public class MainActivity extends Activity {
EditText ed1;
EditText ed2;
EditText ed3;
EditText ed4;
EditText ed5;
EditText ed6;
EditText ed7;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
showinfo();
}
private void showinfo() {
ed1= (EditText) findViewById(R.id.editText1);
ed2= (EditText) findViewById(R.id.editText2);
ed3= (EditText) findViewById(R.id.editText3);
ed4= (EditText) findViewById(R.id.editText4);
ed5= (EditText) findViewById(R.id.editText5);
ed6= (EditText) findViewById(R.id.editText6);
ed7= (EditText) findViewById(R.id.editText7);
btn= (Button) findViewById(R.id.button1);
///////
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String str1 = ed1.getText().toString();
String str2 = ed2.getText().toString();
String str3 = ed3.getText().toString();
String str4 = ed4.getText().toString();
String str5 = ed5.getText().toString();
String str6 = ed6.getText().toString();
String str7 = ed7.getText().toString();
////////////
Toast.makeText(getApplicationContext(), ,Toast.LENGTH_LONG).show();
}
});
}
如何在Toast
?
答案 0 :(得分:3)
如果您想在一个EditText
中显示Toast
的所有内容,那么只需使用连接
String concatenatedText = str1 + str2 + str3 + ...
或StringBuilder
课程:
StringBuilder sb = new StringBuilder();
sb.append(str1);
sb.append(str2);
sb.append(str3);
String concatenatedText = sb.toString();
并简单地将结果作为第二个参数传递,如下所示:
Toast toast = Toast.makeText(context, concatenatedText, duration);
答案 1 :(得分:0)
这是android建议的
f(x)
你可以肯定做类似的事情:
Context context = getApplicationContext(); String text = str1 + str2 + str3 + ...
Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
要走的路是
当 str1 =“你好”; str2 =“Xoce”; str3 =“你好吗?”;
然后吐司中的msg就像:
“你好,你好吗?”;
所以一切都没有给应用程序用户带来任何意义......
StringBuilder sb = new StringBuilder();
sb.append(str1);
sb.append(str2);
sb.append(str3);
String finalMsgText = sb.toString();
Toast toast = Toast.makeText(context, finalMsgText , Toast.LENGTH_SHORT);
toast.show();