2012-02-19 76 views
3

有人能告訴我如何禁用Android中的按鈕,除非所有三個editText字段都被填充了嗎?我不確定在這裏包含條件語句以及如何將它放在按鈕偵聽器的上下文中。禁用Android按鈕,如果文本字段爲空

這是我的主要活動代碼。

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); 

     }; 




    }); 




}  
} 

回答

2

它非常簡單,只需要更新您的onClick()方法,下面的代碼,

@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); 
     } 
    }; 

上述條件將不會t禁用你的按鈕,但只有當你的所有EditText不爲空時它纔會允許。

+0

謝謝!它工作完美。 – Slicekick 2012-02-19 05:13:47

1

怎麼樣:

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

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     if(!"".equals(editTextURL.getText().toString()) 
      && !"".equals(editTextUser.getText().toString()) 
      && !"".equals(editTextPass.getText().toString())){ 

       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); 
     }    
    }; 




}); 

如果要禁用按鈕:

   buttonSubmit.setEnabled(false); //disable button; 
+0

方法等於(字符串)是未定義String類型? – Slicekick 2012-02-19 04:28:47

+0

我把它改成了等號,錯誤消失了;它應該是「等於」嗎? – Slicekick 2012-02-19 04:29:46

+0

對不起,我犯了一個錯誤。這等於 – R4j 2012-02-19 04:31:18

11

可以使用TextChangeListener

EditText ed = //your EditText 
    ed.addTextChangedListener(new TextWatcher() { 

     @Override 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 
      //Check if 's' is empty 

     } 

     @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 

     } 
    }); 
相關問題