2017-01-07 38 views
1

我的目標是打印出一條吐司消息,如果用戶沒有輸入一些數據的話。我已經嘗試了一條if語句,但它似乎不起作用。有什麼建議麼。如果用戶沒有輸入值,我想要顯示一條消息。我嘗試了很多方法

感謝

Data data = new Data(data); 
    FirebaseUser user = firebaseAuth.getCurrentUser(); 
    if(etAddNewTopic.getText().toString() == null || etAddNewTopic.getText().toString() == "") { 
     Toast.makeText(getBaseContext(), "Field cannot be left blank", Toast.LENGTH_SHORT).show(); 
     startActivity(new Intent(this, NewActivity.class)); 
    } else { 
     databaseReference.child(user.getUid()).setValue(data); 
     databaseReference.child("Data List").setValue(data); 
    } 

    progressDialog.setMessage("Adding Data..."); 
    progressDialog.show(); 
    startActivity(new Intent(getApplicationContext(), DataListActivity.class)); 

} 

回答

1

你想:

if(etAddNewTopic.getText().toString() == null || etAddNewTopic.getText().toString().equals("")) { 

if句話的前半部分是好的,但在這裏你看到,如果字符串是空的一半是不完全正確。您當前的代碼使用==比較對象引用,但您想要比較對象值,需要使用equals()方法。閱讀更多here

0

您的查詢完全是Firebase無關緊要的。您在詢問如何確定editText屬性是否已填充或爲空。

這可以很容易實現,下面的代碼位

第一初始化的EditText最後分配

private EditText mName 

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

,我們的註冊功能,當我們確定是否文字已填滿或爲空

private void registerUser() { 

    final String namos = mName.getText().toString(); 


    if(!TextUtils.isEmpty(namos)) { 

    // Text is not empty, register user with FB ! 

     } else { 


    // Text is empty, toast to user they need mind the gap first 
    Toast.maketext(YourClassName.this," Kindly mind the gap ", Toast.LENGTH_SHORT).show(); 

    } 

結果應該是這個樣子

enter image description here

0

您的代碼檢查空字符串:

etAddNewTopic.getText().toString() == "" 

改變它喜歡這一個:

etAddNewTopic.getText().toString().isEmpty() 
1

要扔另一種解決方案是混合,可以使用TextUtils.isEmpty()將和isEmpty()檢查合併爲一條陳述。所以:

if(etAddNewTopic.getText().toString() == null || etAddNewTopic.getText().toString() == "") { 

變爲

if (TextUtils.isEmpty(etAddNewTopic.getText().toString())) { 

我猜敬酒的工作,聲明的罰款之外?

0
if(etAddNewTopic.getText().toString() == null || etAddNewTopic.getText().toString() == "") { 

應該按以下

if(etAddNewTopic.getText().toString().trim().length() < 1) { 
.......... 

這也將避免承擔空間字符串。

相關問題