2012-02-18 3 views
2

onFocusChangeListnerのリストビューでEdittextの検証に問題があります。私はそれが前の読書と比較されます現在の読書を入力するとListView内のEditTextで検証を行う方法は?

私のUIは、この

MeterName Previous CurrentReading 
Meter1 100  Here i want to type current reading 

のように見えます。現在の読解力はそれよりも小さくすることはできません。前回よりも小さい場合は、ユーザーに警告して、同じEditBoxにフォーカスします。

私のコードはここにある:

public void onFocusChange(View v, boolean hasFocus) { 

      try { 
      Previous_Reading = getArray_Meter_Reading.getJSONObject(holder.ref) 
         .getString("PrevMeterReading").toString(); 
      Cumulative = getArray_Meter_Reading 
        .getJSONObject(holder.ref).getString("Cumulative") 
        .toString(); 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 



      if(!hasFocus){ 

       int Current=Integer.valueOf(holder.adp_Current.getText().toString()); 
       Previous=Integer.parseInt(Previous_Reading); 
       if(Current< Previous){ 
        AlertDialog.Builder builder = new AlertDialog.Builder(
          context); 
        builder.setTitle("WARNING"); 
        builder.setIcon(android.R.drawable.ic_dialog_alert); 
        builder.setMessage("Please Enter UserName"); 
        builder.setPositiveButton("ok", 
          new DialogInterface.OnClickListener() { 

           public void onClick(DialogInterface dialog, 
             int which) { 

            // Caption.requestFocus(); 

           } 
          }); 

        AlertDialog diag = builder.create(); 
        diag.show(); 
       } 

      } 

     } }); 
私は以前は600である場合のように入力する内容このコードの

、私はすべての文字のために示されているエディット警告に入力を開始し、私は6のように、現在の読みを入力して私にはalertdialogが表示され、 が再び以前の値よりも大きくなるまで警告が表示されます。

EditTextをクリックするとhasFocusがtrueになり、任意の数値を入力すると、5のフォーカスがfalseに変わります。

サンプルコードを与えることで、検証を行う方法を教えてもらえますか?

答えて

2

thisをご覧ください。その概念をあなたの問題にまで広げることをお勧めします。

EditText myEditText = (EditText)findViewById(R.id.first_name); 
int textValue = 0 ; 
try { 
    textValue = Integer.parseInt(myEditText.getText().toString()) ; 
catch (NumberFormatException nfe) { 
    // Handle the exception 
} 
if (textValue < minValue) { 
    myEditText.requestFocus(); 
    myEditText.setError("Value entered is lower than previous value."); 
} 
+0

。 –

4

あなたの編集用のテキストウォッチャーで使用する方がよいでしょう。一度にエディットボックスから穴のテキストを取得し、私が呼ぶと、それは以前よりも大きくなるまで、エラーメッセージは、すべての数字の入力を要求し得るonfocuschangeListner、任意の単一の番号を入力すると、ので、それは前の値をウィット検証することができますどのように

edittext.addTextChangedListener(new TextWatcher() { 

    @Override 
    public void onTextChanged(CharSequence s, int start, int before, int count) 
       { 
       if(Integer.valueOf(holder.adp_Current.getText().toString()<Integer.parseInt(Previous_Reading)){ 
       edittext.seterror("your error message"); 

      } 

      @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 

      } 
     }); 
+0

しかし、TextWatcherではjava.lang.NumberFormatExceptionが返されます: ''を整数として解析できなくなりました。もう1つはすべての型にすべての型を伝えてエラーメッセージを表示したいのです。 –