2012-09-25 58 views
1

我的代碼不打印空編輯文本itry trim stirng .length == 00但在我的代碼不工作帽錯誤?我的代碼如何在sumbit查詢前檢查edittext是否爲空如何檢查空的編輯文本在android

如果edittext爲空,我想檢查提交方法嗎?如果爲空,然後打印敬酒消息

public class AgAppTransPayExternalAccount extends Activity { 

TextView lblTPEAWelcomeToPayExternalAccountPage; 
TextView lblTPEAOtherAccount; 
TextView lblTPEAPinno; 
TextView lblTPEAAmount; 

EditText txtTPEAotheraccount; 

EditText txtTPEApinno; 

EditText txtTPEAamount; 


Button btnTPEAsubmit; 
Button clearTPEAButton; 
Button btnTPEAgoback; 
String sms; 
public static ProgressDialog PayExternalAccountProgressDialog = null; 
public static boolean value=true; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.agapptranspayexternalaccount); 
    sms=LoginScreen.item.toString(); 

    /* 

    lblTPEAWelcomeToPayExternalAccountPage = (TextView) 
    findViewById(R.id.lblTPEAWelcomeToPayExternalAccountPage); 
    lblTPEAWelcomeToPayExternalAccountPage.setText("Welcome To Pay External 
Account Page"); 


    lblTPEAWelcomeToPayExternalAccountPage.setTextColor(getResources().getColor 
    (R.color.text_color_black)); 
    */ 

    lblTPEAOtherAccount = (TextView) findViewById(R.id.lblTPEAOtherAccount); 
    lblTPEAOtherAccount.setText("Other Account :"); 

    txtTPEAotheraccount=(EditText) findViewById(R.id.txtTPEAotheraccount); 



    lblTPEAPinno = (TextView) findViewById(R.id.lblTPEAPinno); 
    lblTPEAPinno.setText("PIN Number :"); 

    txtTPEApinno=(EditText) findViewById(R.id.txtTPEApinno); 

    lblTPEAAmount = (TextView) findViewById(R.id.lblTPEAAmount); 
    lblTPEAAmount.setText("Amount :"); 

    txtTPEAamount=(EditText) findViewById(R.id.txtTPEAamount); 

    btnTPEAsubmit=(Button) findViewById(R.id.btnTPEAsubmit); 

    btnTPEAsubmit.setTextColor(getResources().getColor(R.color.text_color_blue)); 

    clearTPEAButton=(Button) findViewById(R.id.clearTPEAButton); 

    clearTPEAButton.setTextColor(getResources().getColor(R.color.text_color_blue)); 

    btnTPEAgoback=(Button) findViewById(R.id.btnTPEAgoback); 

    btnTPEAgoback.setTextColor(getResources().getColor(R.color.text_color_blue)); 


    clearTPEAButton.setOnClickListener(new OnClickListener() 
    { 
    public void onClick(View v) 

    { 
     txtTPEAotheraccount.setText(""); 
     txtTPEApinno.setText(""); 
     txtTPEAamount.setText(""); 

      } 
     }); 

    btnTPEAgoback.setOnClickListener(new OnClickListener() 
    { 
    public void onClick(View v) 

    { 
     finish(); 

      } 
     }); 
    btnTPEAsubmit.setOnClickListener(new OnClickListener() 
    { 
    public void onClick(View v) 

    { 
     String tpeapinemptycheck = txtTPEApinno.getText().toString(); 
     String otheraccountemptycheck = 
        lblTPEAOtherAccount.getText().toString(); 
     String amountemptycheck = txtTPEAamount.getText().toString(); 

     if (tpeapinemptycheck.trim().equals("")|| 
    (otheraccountemptycheck.trim().equals("")) ||(amountemptycheck.trim().equals(""))) 
     { 
     Toast.makeText(getApplicationContext(), "Please Enter 
    Correct Information", Toast.LENGTH_LONG).show(); 
        } 

     else 
        showProgress(); 
       submitPEA(); 

    } 
    }); 

    } 

private void submitPEA() { 
    String message; 
     String mobilenumber= LoginScreen.smsmobileno; 

     if ((sms.compareTo("SMS")==0)) 
     { 
    SmsManager smsmanager = SmsManager.getDefault(); 

message="AGPEA"+AgAppHelperMethods.varMobileNo+AgAppHelperMethods. 

    arMobileNo+txtTPEAotheraccount.getText().toString()+AgAppHelperMethods. 
varMobileNo+txtTPEApinno.getText().toString()+txtTPEAamount.getText().toString(); 
    smsmanager.sendTextMessage(mobilenumber, null, message, null, null); 
     } 
else 
    { 

     Intent j = new Intent(AgAppTransPayExternalAccount.this, AgAppTransPEAResponse.class); 


     Bundle bundle = new Bundle(); 
     bundle.putString("txtTPEApinno", txtTPEApinno.getText().toString()); 


    bundle.putString("txtTPEAotheraccount",txtTPEAotheraccount.getText().toString()); 
     bundle.putString("txtTPEAamount",txtTPEAamount.getText().toString()); 
     j.putExtras(bundle); 


     startActivity(j); 
     value=false; 
     PayExternalAccountProgressDialog.dismiss(); 

} 


    } 
private void showProgress() 
{  

    PayExternalAccountProgressDialog = 
ProgressDialog.show(AgAppTransPayExternalAccount.this,null, "Processing please 
    wait...", true);  
    if (PayExternalAccountProgressDialog != null) { 
     try 
     { 
     Handler handler = new Handler(); 
     handler.postDelayed(new Runnable() 
     { 
     @Override 
     public void run() 
     { 
      PayExternalAccountProgressDialog.dismiss(); 
      if(value) 
      { 
      Toast.makeText(AgAppTransPayExternalAccount.this, "Request 
     TimeOut " , Toast.LENGTH_SHORT).show(); 
      } 
     } 
     }, 15000); // <--- here is the time adjustment. 
     } 
     catch (Exception e) 
     { 
     } 
     } 
} 
     } 
+1

由於您是Stack Overflow的新手,請訪問此[如何接受答案](http://meta.stackexchange.com/a/65088) – Lucifer

回答

4

你的代碼是正確的,唯一缺少的,這是在其他條件{ }牙套,嘗試如下,

if (tpeapinemptycheck.trim().equals("")|| 
     (otheraccountemptycheck.trim().equals("")) ||(amountemptycheck.trim().equals(""))) 
{ 

    Toast.makeText(getApplicationContext(), "Please Enter 
      Correct Information", Toast.LENGTH_LONG).show(); 


} 

else 
{ // add this 
    showProgress(); 
    submitPEA(); 
} // add this 

只是因爲你還沒有加入這些{ }大括號,您的控制權將進入submitPEA()方法。

+0

但不勾選空編輯文本didnow顯示敬酒信息請輸入corrent information –

+0

只是試試我的答案親愛的 – Lucifer

+0

是的,它的工作非常棒 –

1

嘗試這樣

edit_text.getText().toString().trim().equals(""); 
+0

如何檢查3編輯文本imean如何使用OR在多個edittext視圖中如果有人是空的顯示錯誤 –

+0

看到我更新的答案.... –

-1

創建一個字符串變量例如x; 現在如果et是您的EditText字段,請使用以下代碼:

x = et.getText()。toString();

如果EditText字段中有任何文本,它將被傳遞給字符串x。 現在,以檢查串x不爲空或不包含任何使用

if(x.matches("")) 
{ 
//your code here 
} 
else 
{ 
//the counter action you'll take 
} 

這樣你可以檢查你是有關數據庫中輸入的條目將不能爲空。 快樂編碼。

相關問題