2015-06-19 33 views
0

我想做一個單位轉換器,它似乎它的工作,我只需要給他們一個錯誤,如果他們不把任何數字放在第一個領域和其他錯誤,如果他們把一個數字放在第二個領域(所以他們不能向後將其轉換)如果Java上有一個空白字段,我該如何處理一個錯誤消息?

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     final EditText editCentimeters = (EditText) findViewById(R.id.editCentimeters); 

     final EditText editInches = (EditText) findViewById(R.id.editInches); 

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

     buttonConvert.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 

       double centimeters = Double.valueOf(editCentimeters.getText().toString()); 

       double inches = centimeters * 0.393700787 ; 

       editInches.setText(String.valueOf(inches)); 

      } 
     }); 



    } 

} 
+0

是的,您可以使用['Toast'](http://developer.android.com/guide/topics/ui/notifiers/toasts.html)來達到此目的。 –

+0

editinches應ba textview,以便用戶不能編輯或添加任何值...下一次使用應該做一個檢查,如果editcentimeter是空的,如果是你顯示錯誤...可以是Alertdialog,烤麪包以顯示錯誤消息 – Psypher

+0

根據現代指導方針,最好使用「Snackbar」而不是「Toast」。 – SqueezyMo

回答

0

使用SETERROR()這個方法。做財產以後這樣

buttonConvert.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 

      if(editCentimeters.getText().toString().equals("")){ 

       editCentimeters.setError("*Field Required"); 

      }else{ 

      double centimeters = Double.valueOf(editCentimeters.getText().toString()); 

      double inches = centimeters * 0.393700787 ; 

      editInches.setText(String.valueOf(inches)); 

      } 

     } 
    }); 
0

您必須檢查每個EditText自己裏面的值,如果其空通知用戶:

buttonConvert.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View arg0) { 
     if(!"".equals(editCentimeters.getText().toString())) { 
      // If the field is not empty, proceed to calculation 
      double centimeters = Double.valueOf(editCentimeters.getText().toString()); 
      double inches = centimeters * 0.393700787 ; 
      editInches.setText(String.valueOf(inches)); 
     } else { 
      // If it's empty, show a message to the user) 
      editCentimeters.setError("This field can not be empty"); 
     } 
    } 
}; 
1

你可以用敬酒給一個錯誤信息。 但我使用EditText.setError()方法。因此用戶將能夠確切地看到哪個字段負責驗證問題 單擊HERE for a tutorialHere for another solid tutorial

這是一個示例代碼(我沒有ADT在這裏,所以原諒我,如果它需要一些修正):

 final EditText editCentimeters = (EditText) findViewById(R.id.editCentimeters); 
     final EditText editInches = (EditText) findViewById(R.id.editInches); 




     @Override 
     public void onClick(View arg0) { 

     boolean isValid = true; 
     if (editCentimeters.getText().toString().isEmpty()) 
     { 
      editCentimeters.setError("This input cannot be null"); 
      isValid = false; 
     } 
     if (editInches.getText().toString().isEmpty()) 
     { 
      editInches.setError("This input cannot be null"); 
      isValid = false; 
     } 

     if (isValid) 
     {   
      double centimeters = Double.valueOf(editCentimeters.getText().toString()); 

      double inches = centimeters * 0.393700787 ; 

      editInches.setText(String.valueOf(inches)); 
     } 

    } 
0

的吐司類將是適用於該目的爲: -

if(editText.getText().toString().equals("")) 
    Toast.makeText(this,"Your Error msg",Toast.LENGTH_LONG).show(); 

第一個參數是你的活動
二的上下文是爲CharSequence
;第三是吐司的持續時間。

0

editinches應該是textview,以便用戶不能編輯或添加任何值...下一個使用應該做一個檢查,如果editcentimeter爲空,如果是的話,你顯示一個錯誤...可以是Alertdialog,toast顯示錯誤信息。

if(editcentimeter.isEmpty(){ 
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context); 

     // set title 
     alertDialogBuilder.setTitle("Missing value"); 

     // set dialog message 
     alertDialogBuilder 
      .setMessage("Please enter a value"); 

     // set title 
     alertDialogBuilder.setTitle("Your Title"); 

     // set dialog message 
     alertDialogBuilder 
      .setMessage("Please enter a numbert!") 
      .setCancelable(false) 
      .setPositiveButton("Yes",new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog,int id) { 
        dismiss(); 
       } 
       }); 
     } 
} 
相關問題