如果必填字段为空,如何禁用Button

时间:2014-04-25 11:43:42

标签: android

在我的注册表格中,如果任何必填字段为空,如何禁用注册button?填写所有必填字段后,它必须转到数据库。我收到错误消息,但没有得到如何禁用Button。这是我的代码,

public class MainActivity extends Activity implements View.OnClickListener{
Button log,sign;
private EditText firstname,lastname,mycity,myphone,password,cpassword;
Spinner bloodgroup,myarea;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.register);
    log = (Button) findViewById(R.id.register);
    sign = (Button) findViewById(R.id.linktologin);
    log.setOnClickListener(this);
    sign.setOnClickListener(this);
    initializeVars();
    bloodgroup = (Spinner) findViewById(R.id.bgroup) ;
    List<String> list = new ArrayList<String>();
    list.add("A+");
    list.add("A-");
    list.add("B+");
    list.add("B-");
    list.add("O+");
    list.add("O-");
    list.add("AB+");
    list.add("AB-");
    ArrayAdapter adapter = new ArrayAdapter(this,
            android.R.layout.simple_spinner_item, list);
    bloodgroup.setAdapter(adapter);

    myarea = (Spinner) findViewById(R.id.area) ;
    List<String> mylist = new ArrayList<String>();
    mylist.add("Vijaynagar");
    mylist.add("Malleshwarm");
    mylist.add("Banashankari");
    mylist.add("Adugodi");
    ArrayAdapter adapt = new ArrayAdapter(this,
            android.R.layout.simple_spinner_item, mylist);
    myarea.setAdapter(adapt);

}
private void initializeVars() {
    // TODO Auto-generated method stub
    firstname = (EditText) findViewById(R.id.fname);
    lastname = (EditText) findViewById(R.id.lname);
    mycity = (EditText) findViewById(R.id.city);
    myphone = (EditText) findViewById(R.id.phone);
    password = (EditText) findViewById(R.id.paswrd);
    cpassword = (EditText) findViewById(R.id.cpaswrd);
}


public void onClick(View arg0) {
    switch(arg0.getId()) {
    case R.id.linktologin:
        Intent in = new Intent(MainActivity.this,Register.class);
        startActivity(in);
        break;

    case R.id.register:
        Boolean diditwork=true;
        try{
            EditText firstname = (EditText) findViewById(R.id.fname);
            String name=firstname.getText().toString();
            if (name.matches("")) {
                Toast.makeText(this, "You did not enter a username", Toast.LENGTH_SHORT).show();
                return;
            }
            EditText mycity = (EditText) findViewById(R.id.city);
            String mycty=mycity.getText().toString();
            if (mycty.matches("")) {
                Toast.makeText(this, "You did not enter your city", Toast.LENGTH_SHORT).show();
                return;
            }
            EditText myphone = (EditText) findViewById(R.id.phone);
            String ph=String.valueOf(myphone);
            if (ph.matches("")) {
                Toast.makeText(this, "You did not enter your phone number", Toast.LENGTH_SHORT).show();
                return;
            }
            EditText password = (EditText) findViewById(R.id.paswrd);
            String psd=password.getText().toString();
            if (psd.matches("")) {
                Toast.makeText(this, "You did not enter password", Toast.LENGTH_SHORT).show();
                return;
            }
            cpassword = (EditText) findViewById(R.id.cpaswrd);
            String cpsd=cpassword.getText().toString();
            if (cpsd.matches("")) {
                Toast.makeText(this, "please confirm your password", Toast.LENGTH_SHORT).show();
                return;
            }

            String lname=lastname.getText().toString();
            String bg =bloodgroup.getSelectedItem().toString();
            String are= myarea.getSelectedItem().toString();


            Database entry = new Database(MainActivity.this);
            entry.open();
            entry.createEntry(name, lname, bg, are, mycty, ph, psd, cpsd);
            entry.close();

        }
        catch (Exception e){
            diditwork=false;
            String error=e.toString();
            Dialog d=new Dialog(this);
            d.setTitle("Somewhere you are wrong jus check..");
            TextView tv=new TextView(this);
            tv.setText("Sucess");
            d.setContentView(tv);
            d.show();
            break;
        }

    }}  }

谢谢

6 个答案:

答案 0 :(得分:1)

这样做:

button.setEnabled(false);

答案 1 :(得分:1)

使用:

myButton.setEnabled(false);

此外,可以通过xml使用android:clickable来设置按钮是否可以点击。

点击此链接here

答案 2 :(得分:0)

检查空字段后,只需添加此行

sign.setEnabled(false);

用于签名按钮和

log.setEnabled(false)

用于日志按钮。

答案 3 :(得分:0)

您必须向所有视图添加侦听器。每次编辑后,您应检查结果并使用setEnabled(boolean)更新按钮状态。

答案 4 :(得分:0)

使用以下代码来完成您的任务: -

    firstname.addTextChangedListener(new TextWatcher()
    {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count)
        {
            // TODO Auto-generated method stub
            if(s.length()>0)
            {
                log.setEnabled(false);
            }

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after)
        {
            // TODO Auto-generated method stub
        }

        @Override
        public void afterTextChanged(Editable s)
        {
            // TODO Auto-generated method stub
        }
    });

答案 5 :(得分:0)

继续@Siddharth的回答,我想补充一些代码。

我看到你有一堆你要验证的EditText。如果要填写所有必填字段时启用注册button,那么解决方案就是添加全局倾听它。

示例:

为活动的根视图指定一个已知ID,例如'@ + id / activityRoot',将GlobalLayoutListener挂钩到ViewTreeObserver

final View activityRootView = findViewById(R.id.activityRoot);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
       //Give your all  validation conditions here
       if(firstname.getText().toString().equals("")
         {
          button.setEnabled(false);
         }

     }
});

您可以在那里提供所有必须的EditText条件。请务必在此之前初始化EdiText,否则您将获得NullPointerException

参考 http://developer.android.com/reference/android/view/ViewTreeObserver.html

https://stackoverflow.com/a/4737265/1665507