简体   繁体   English

如何检查我的 editText 字段是否为空并使其转到带有 Button 的新活动?

[英]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.

相关问题 单击按钮后,如何将文本从alertdialog的EditText复制并粘贴到活动的EditText中? - How do I copy and paste text from EditText of alertdialog to EditText of my activity, on button click? 单选按钮应该打开新活动,只有当edittext字段不为空时 - Radio button should open new activity, only when edittext field is not empty 按钮 continueButton 不工作? 如果我在 EditText 中有文本或制作 Toast,它应该启动新的 Activity - Whe button continueButton isnt working? It should start the new Activity if I have a text in EditText or make a Toast 如何检查edittext是否为空? - How can I check if edittext is empty? 使用按钮检查 EditText 是否为空 - Using a button to check if EditText is empty 如何使EditText显示我的文本输入? - How do I make EditText show my text input? 你如何在 android 中创建一个按钮来打开一个新的活动,这个活动不仅仅是一个默认活动,它是一个自定义活动 - How do you make a button in android that opens a new activity and this activity is not just a default activity its a custom one 每当我的EditText为空并且按下按钮时,我的应用程序就会崩溃 - My app crashes whenever my EditText is empty and I press a button 如何使 EditText 不为空? - How to make EditText not empty? 如何使EditText具有一定的最大宽度,但不离开屏幕? - How do I make an EditText have a certain maximum width, but not go off the screen?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM