如何使用EditText的内容显示Toast

时间:2017-01-11 11:03:48

标签: android onclicklistener toast

我正在使用EditText和Button,当单击Button时,EditText的内容应该显示在Toast消息中。

这是我到目前为止所尝试的内容:

public void  cLickFuntion(View view){
    EditText name=(EditText)findViewById(R.id.TvName);
    Toast.makeText(getApplicationContext(),"Hello"+ name.getText().toString(),Toast.LENGTH_SHORT).show();
}

4 个答案:

答案 0 :(得分:3)

你可以像这样打电话

def make_poly(coefs):
    def poly(x):
        result = 0
        for c in coefs:
            result = result * x + c
        return result
    return poly

p = make_poly([2,3,1])

print(p(0))
print(p(42))

答案 1 :(得分:2)

onCreate中添加代码:

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_layout); 

    EditText name = (EditText)findViewById(R.id.TvName);
    Button One = (Button) findViewById(R.id.your_id);
    One.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(),"Hello"+ name.getText().toString(),Toast.LENGTH_SHORT).show();
        }
    });
 }

答案 2 :(得分:0)

将此行添加到布局中的按钮xml

android:onClick="cLickFuntion"

答案 3 :(得分:0)

final EditText editText1 = findViewById(R.id.edittext1);
    final EditText editText2=findViewById(R.id.editText2) ;
    Button button=findViewById(R.id.button) ;
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Toast.makeText(getApplicationContext(),"your user name"+editText1.getText().toString(),Toast.LENGTH_LONG).show();
            Toast.makeText(getApplicationContext(),"your password"+editText2.getText().toString(),Toast.LENGTH_LONG).show();
        }
    });