据我所知,这将存储用户并传入我制作的变量。然后获取文本并将其转换为字符串。现在如果发生这种情况,我怎么称呼时只出现空白气泡?< / p>
package com.set.ultimax.login;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Main extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button logIn;
EditText user;
EditText pass;
logIn = (Button) findViewById(R.id.btn);
user = (EditText) findViewById(R.id.username);
pass = (EditText) findViewById(R.id.password);
final String userValue = user.getText().toString();
final String passValue = pass.getText().toString();
logIn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
Toast.makeText(Main.this,userValue, Toast.LENGTH_LONG).show();
Toast.makeText(Main.this,passValue, Toast.LENGTH_LONG).show();
}
});
}
}
答案 0 :(得分:1)
您必须在点击按钮时从EditText
获取文字,而不是onCreate
上的文字。
试试这个:
logIn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
final String userValue = user.getText().toString();
final String passValue = pass.getText().toString();
Toast.makeText(Main.this,userValue, Toast.LENGTH_LONG).show();
Toast.makeText(Main.this,passValue, Toast.LENGTH_LONG).show();
}
});
答案 1 :(得分:1)
不要在onCreate中获取值,在侦听器中执行此操作,并且在它们将重叠时不要连续显示两个toast。像这样:
logIn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
EditText user = (EditText) findViewById(R.id.username);
EditText pass = (EditText) findViewById(R.id.password);
String userValue = user.getText().toString();
String passValue = pass.getText().toString();
Toast.makeText(Main.this,"User: " + userValue + " Pass: " + passValue, Toast.LENGTH_LONG).show();
}
});