[英]How to pass edit Text value to Toast?
I've just started with android and I'm trying to make simple program to take string from user and display the result as Toast
我刚开始使用android,并且我正在尝试制作一个简单的程序来从用户那里获取字符串并将结果显示为Toast
EditText e = (EditText) findViewById(R.id.editText);
final String result = e.getText().toString();
Button btx = (Button) findViewById(R.id.button2);
btx.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
}
Now If I type anything in editText
it's supposd to print that value but instead it's printing the default text value and even if I edit that it prints the same value 现在,如果我在editText
键入任何内容,则可以打印该值,但是它将打印默认的文本值,即使我编辑它也可以打印相同的值
As you can see in the picture on pressing the button it shows "Name" on toast 如您在按下按钮时所看到的那样,它在吐司上显示“名称”
and even upon changing it shows the same thing that is "Name". 即使更改,它也显示出与“名称”相同的内容。 I want it too show the value that I typed later. 我希望它也显示我稍后输入的值。 What should I do? 我该怎么办?
You store the text when you setup the views. 您在设置视图时存储文本。 Thats why you get the default text. 那就是为什么您得到默认文本。
move 移动
final String result=e.getText().toString();
into the onClick
进入onClick
@Override
public void onClick(View v) {
final String result = e.getText().toString();
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
and it should get the text when the button is pressed. 当按下按钮时,它应该获得文本。
Reason is your variable String result
is not taking latest value when button is clicked. 原因是单击按钮时,您的变量String result
未采用最新值。
Try this: 尝试这个:
final EditText e = (EditText) findViewById(R.id.editText);
Button btx = (Button) findViewById(R.id.button2);
btx.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String result = e.getText().toString();
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
}
your requirement is show EditText
Text in Toast 您的要求是在Toast中显示EditText
Text
first make EditText
object as globle 首先使EditText
对象为globle
class ActivityClassFileName extend AppCompactActivity
{
EditText ToastText;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activitylayout);
//initialize EditText object
ToastText = (EditText) findViewById(R.id.editText);
Button btx = (Button) findViewById(R.id.button2);
btx.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
//showing Toast
Toast.makeText(getApplicationContext(), ToastText.getText().toString(), Toast.LENGTH_LONG).show();
}
}
}
}
Sometimes you have to display toast outside of onclick,by creating new method. 有时,您必须通过创建新方法来在onclick之外显示烤面包。
public class MainActivity extends AppCompatActivity {
EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Button button = findViewById(R.id.button);
editText=findViewById(R.id.editTextView);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String testString=editText.getText().toString();
showToast(testString);
}
});
}
private void showToast(String str) {
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.