[英]How do I check if my editText field is empty and make it go to a new activity with a Button?
I have made this code :我已经做了这个代码:
public class MainActivity extends AppCompatActivity {
private ImageButton ourButton;
private EditText operand1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find Views by IDs :
ourButton = (ImageButton) findViewById(R.id.gobutton);
operand1 = (EditText) findViewById(R.id.edit_Name);
String ed_text = operand1.getText().toString().trim();
if(ed_text.isEmpty() || ed_text.length() == 0 || ed_text.equals("") || ed_text == null)
{
ourButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "You did not enter a username", Toast.LENGTH_LONG).show();
}
});
}
else
{
ourButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(MainActivity.this, ScreenName.class);
MainActivity.this.startActivity(myIntent);
}
});
}
}
The problem is that if I pressed the button with or without entering anything in the field, it will say :问题是,如果我在字段中输入或不输入任何内容的情况下按下按钮,它会说:
You did not enter a username
您没有输入用户名
And the next activity will not work neither.下一个活动也不会起作用。
How do I make the button to move the user into the next activity while making sure he inputed a name?如何制作按钮以将用户移至下一个活动,同时确保他输入了姓名?
Use this, I have modified your code..
public class MainActivity extends AppCompatActivity {
private ImageButton ourButton;
private EditText operand1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find Views by IDs :
ourButton = (ImageButton) findViewById(R.id.gobutton);
operand1 = (EditText) findViewById(R.id.edit_Name);
ourButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String ed_text = operand1.getText().toString().trim();
if(ed_text.equals(""))
{
Toast.makeText(MainActivity.this, "You did not enter a username", Toast.LENGTH_LONG).show();
}
else
{
Intent myIntent = new Intent(MainActivity.this, ScreenName.class);
MainActivity.this.startActivity(myIntent);
}
}
});
}
The problem is that you are not checking the value in the listener.问题是您没有检查侦听器中的值。 Change it to this:
改成这样:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find Views by IDs :
ourButton = (ImageButton) findViewById(R.id.gobutton);
operand1 = (EditText) findViewById(R.id.edit_Name);
ourButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (operand1.getText().toString().trim().length() > 0) {
Intent myIntent = new Intent(MainActivity.this, ScreenName.class);
MainActivity.this.startActivity(myIntent);
} else {
Toast.makeText(MainActivity.this, "You did not enter a username", Toast.LENGTH_LONG).show();
}
}
});
}
Also onCreate gets called when the Activity is created so you can't check the name field's value in there.在创建 Activity 时也会调用 onCreate,因此您无法在其中检查 name 字段的值。 That's why you need to move that check in your button's click listener.
这就是为什么您需要在按钮的单击侦听器中移动该检查。 Then it will always check if the name field is empty when you click your button.
然后,当您单击按钮时,它会始终检查名称字段是否为空。
You need to do the check when the button is pressed, here is an example:您需要在按下按钮时进行检查,这是一个示例:
public class MainActivity extends AppCompatActivity {
private ImageButton ourButton;
private EditText operand1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find Views by IDs :
ourButton = (ImageButton) findViewById(R.id.gobutton);
operand1 = (EditText) findViewById(R.id.edit_Name);
ourButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(operand1.getText().length() == 0){
Toast.makeText(MainActivity.this, "You did not enter a username", Toast.LENGTH_LONG).show();
}else{
Intent myIntent = new Intent(MainActivity.this, ScreenName.class);
MainActivity.this.startActivity(myIntent);
}
}
});
}
}
Try this instead...试试这个...
public class MainActivity extends AppCompatActivity {
private ImageButton ourButton;
private EditText operand1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find Views by IDs :
ourButton = (ImageButton) findViewById(R.id.gobutton);
operand1 = (EditText) findViewById(R.id.edit_Name);
String ed_text = operand1.getText().toString().trim();
ourButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(ed_text.isEmpty() || ed_text.length() == 0 || ed_text.equals("") || ed_text == null) {
Toast.makeText(MainActivity.this, "You did not enter a username", Toast.LENGTH_LONG).show();
} else {
Intent myIntent = new Intent(MainActivity.this, ScreenName.class);
MainActivity.this.startActivity(myIntent);
}
});
}
}
You can try this code:你可以试试这个代码:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private ImageButton ourButton;
private EditText operand1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find Views by IDs :
ourButton = (ImageButton) findViewById(R.id.gobutton);
operand1 = (EditText) findViewById(R.id.edit_Name);
ourButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int id = v.getId();
if (id == R.id.gobutton) {
if(operand1.getText().toString().trim().equals("")){
Toast.makeText(MainActivity.this, "You did not enter a username", Toast.LENGTH_LONG).show()
}else{
Intent myIntent = new Intent(this,ScreenName.class);
startActivity(myIntent);
}
}
}
cahnge your code like that像这样改变你的代码
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private ImageButton ourButton;
private EditText operand1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find Views by IDs :
ourButton = (ImageButton) findViewById(R.id.gobutton);
operand1 = (EditText) findViewById(R.id.edit_Name);
ourButton.setOnClickListener(this);
}
@Override
public void onClick(View view) {
String ed_text = operand1.getText().toString().trim();
if(ed_text.equals("")){
Toast.makeText(MainActivity.this, "You did not enter a username", Toast.LENGTH_LONG).show()
}else{
Intent myIntent = new Intent(MainActivity.this, ScreenName.class);
MainActivity.this.startActivity(myIntent);
}
}
}
you can use this你可以用这个
public class MainActivity extends AppCompatActivity {
private ImageButton ourButton;
private EditText operand1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find Views by IDs :
ourButton = (ImageButton) findViewById(R.id.gobutton);
operand1 = (EditText) findViewById(R.id.edit_Name);
String ed_text = operand1.getText().toString();
ourButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(ed_text.equalsignorcase("")
{
Toast.makeText(MainActivity.this, "You did not enter a username", Toast.LENGTH_LONG).show();
}
else
{
Intent myIntent = new Intent(MainActivity.this, ScreenName.class);
MainActivity.this.startActivity(myIntent);
}
});
}
Don't set two OnClickListeners for one button不要为一个按钮设置两个 OnClickListeners
Try following code -尝试以下代码 -
String ed_text = operand1.getText().toString().trim();
ourButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!TextUtils.isEmpty(ed_text)){
Toast.makeText(MainActivity.this, "Not Empty", Toast.LENGTH_SHORT).show();
Intent myIntent = new Intent(MainActivity.this, ScreenName.class);
startActivity(myIntent);
finish(); //If you want to kill current Activity.
}else{
Toast.makeText(MainActivity.this, "Empty", Toast.LENGTH_SHORT).show();
}
}
});
Hope it will help :)希望它会有所帮助:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.