If the text field is empty, the Android button is disabled

Can someone tell me how to disable buttons in Android unless all three EditText fields are filled? I don't know where to include the conditional statement here and how to put it in the context of the button listener

This is my main activity code

package com.amritayalur.mypowerschool;



import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;



public class MyPowerSchoolActivity extends Activity {
Button buttonSubmit;
TextView textViewTitle;
TextView textViewDesc;
EditText editTextURL, editTextUser, editTextPass;

String url = "";
 String str;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    buttonSubmit = (Button) findViewById(R.id.buttonSubmit);
    textViewTitle = (TextView) findViewById(R.id.textViewTitle);
    textViewDesc = (TextView) findViewById(R.id.textViewDesc);

    editTextURL = (EditText) findViewById(R.id.editTextURL);
    editTextUser = (EditText) findViewById(R.id.editTextUser);
    editTextPass = (EditText) findViewById(R.id.editTextPass);
    //Start TextView
    textViewTitle.setText("MyPowerSchool");


    //button listener
    buttonSubmit.setOnClickListener(new View.OnClickListener() {


        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent i = new Intent( MyPowerSchoolActivity.this, creds.class);    
            i.putExtra("pschoolurl", editTextURL.getText().toString());
            i.putExtra("pschooluser", editTextUser.getText().toString());
            i.putExtra("pschoolpass", editTextPass.getText().toString());
            // get the text here
            final int result = 1;
            startActivityForResult(i, result); 

        };




    });




}       
}

resolvent:

It's very simple. Just update your onclick () method with the following code,

    @Override
    public void onClick(View v) 
    {
        if (  ( !editTextURL.getText().toString().equals("")) && ( !editTextUser.getText().toString().equals("")) && ( !editTextPass.getText().toString().equals("") ) ) 
        {
             // TODO Auto-generated method stub
            Intent i = new Intent( MyPowerSchoolActivity.this, creds.class);    
            i.putExtra("pschoolurl", editTextURL.getText().toString());
            i.putExtra("pschooluser", editTextUser.getText().toString());
            i.putExtra("pschoolpass", editTextPass.getText().toString());
           // get the text here
           final int result = 1;
           startActivityForResult(i, result); 
        }
    };

The above if condition does not disable the button, but is allowed only if all EditText is not empty

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>