简体   繁体   English

获取一个EditText到String然后在Toast中显示

[英]Get a EditText to String then show in Toast

I'm very new in Java and have been trying this for days now, seems that I need to profundize the bases way more, anyway, it seems that I can't get the toast right, here's the code. 我是Java的新手,并且已经尝试了好几天了,看来我需要进一步加大基础的投入,无论如何,看来我做不了什么,这是代码。

public class MainActivity extends AppCompatActivity {
    EditText web;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        Button button = findViewById(R.id.button);
        final EditText web = findViewById(R.id.web);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String webastring = web.getText().toString();
                Toast.makeText((this,web.toString(), "asd", Toast.LENGTH_SHORT).show();
            }
        });

    }
}

Update button.setOnClickListener() method by following piece of code 通过以下代码更新button.setOnClickListener()方法

button.setOnClickListener(new View.OnClickListener() { 
@Override public void onClick(View v) 
{ 
    String webastring = web.getText().toString();     
    Toast.makeText(getApplicationContext(),webastring,Toast.LENGTH_SHORT).show(); 
}});

In order to get an EditText's (or any other child of TextView) contents, you have to call the function getText() . 为了获得EditText(或TextView的任何其他子级)的内容,必须调用函数getText()

So your code will be like this: 因此,您的代码将如下所示:

        Toast.makeText((this,web.getText().toString(), "asd", Toast.LENGTH_SHORT).show();

Update button setOnClickListener as below code: 更新button setOnClickListener如下代码:

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        String webastring = web.getText().toString();
        Toast.makeText(MainActivity.this ,webastring, Toast.LENGTH_SHORT).show();
    }
});

You have to use MainActivity.this instead of only this. 您必须使用MainActivity.this而不是仅使用this。 Here the 'this' represents anonymous class not MainActivity. 这里的“ this”代表匿名类而不是MainActivity。 But you have to pass main activity reference to Toast. 但是您必须将主要活动参考传递给Toast。 You also put four parameters instead of three to Toast.makeText() method. 您还将四个参数(而不是三个)放到Toast.makeText()方法中。 So you have to pass three parameters. 因此,您必须传递三个参数。 Here is the official link https://developer.android.com/guide/topics/ui/notifiers/toasts for showing Toast. 这是用于显示Toast的官方链接https://developer.android.com/guide/topics/ui/notifiers/toasts I hope the following code will be worked fine for you. 我希望以下代码对您来说可以正常工作。

button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String webstring = web.getText().toString();
                Toast.makeText(MainActivity.this, webstring + "asd", Toast.LENGTH_SHORT).show();
            }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM