[英]Stuck with Android studio
I am currently building an app where the user enters their name and when you press the button it should say "Welcome, (whatever name the user entered) in a Toast. 我正在构建一个用户输入其名称的应用程序,当您按下按钮时,它应该在Toast中显示“欢迎,(用户输入的任何名称)”。
I feel like I have everything write but it's not working. 我觉得我有一切都写,但它不起作用。 It lets me type in my name but when I click the button nothing happens.
它让我输入我的名字但是当我点击按钮时没有任何反应。 What am I doing wrong?
我究竟做错了什么?
Here's my code 这是我的代码
public class MainActivity extends AppCompatActivity { 公共类MainActivity扩展AppCompatActivity {
private EditText input;
private Button click;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
input = (EditText) findViewById(R.id.editText5);
click = (Button) findViewById(R.id.outputBTN);
click.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), input.getText(). "Welcome ", Toast.LENGTH_SHORT).show();
}
});
}
} }
You have to use the toString();
你必须使用
toString();
method because input.getText()
returns you an Editable Object! 方法因为
input.getText()
返回一个可编辑对象!
With the toString()
you can convert your Object into a String. 使用
toString()
您可以将Object转换为String。
See getText() method: https://developer.android.com/reference/android/widget/EditText.html 请参阅getText()方法: https : //developer.android.com/reference/android/widget/EditText.html
Solution: 解:
Toast.makeText(getApplicationContext(),"Welcome," + input.getText().toString(), Toast.LENGTH.SHORT).show();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.