2017-11-08 12 views
1

내 응용 프로그램에서는 어색한 시나리오가 있습니다.edit 텍스트 텍스트 select-all을 입력 한 다음 첫 번째 문자를 입력하면 표시되지 않습니다.

내 애플리케이션의 가격은 EditText이며 가격을 입력하면 모든 텍스트가 선택되고 가격을 입력 할 수 있습니다. 사용자가 EditText을 클릭하면 모든 텍스트가 선택되지만 사용자가 텍스트를 입력하면 첫 번째 숫자는 표시되지 않습니다. 그러면 다른 번호가 올바르게 표시됩니다. TextChangeListner을 추가하고 알아 내려고했지만 사용자가 첫 번째 문자를 입력하는 동안 CharSequence을 입력하는 중입니다.

EditText에 대한 입력 필터가 있습니다. 아래의 editfilter 코드를 확인하십시오.

public static class InputFilterForDoubleMinMax implements InputFilter { 

    private double min, max; 

    public InputFilterForDoubleMinMax(double min, double max) { 
     this.min = min; 
     this.max = max; 
    } 

    public InputFilterForDoubleMinMax(String min, String max) { 
     this.min = Double.parseDouble(min); 
     this.max = Double.parseDouble(max); 
    } 

    @Override 
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { 
     try { 
      String temp = dest.toString() + source.toString(); 
      if (source.toString().equalsIgnoreCase(".")) { 
       return ""; 
      } else if (temp.toString().indexOf(",") != -1) { 
       temp = temp.toString().substring(temp.toString().indexOf(",") + 1); 
       if (temp.length() > 2) { 
        return ""; 
       } 
      } 


      double input = Double.parseDouble(dest.toString().replace(",", ".").replace("€", "") + source.toString().replace(",", ".").replace("€", "")); 
      if (isInRange(min, max, input)) 
       return null; 
     } catch (NumberFormatException nfe) { 
     } 
     return ""; 
    } 

    private boolean isInRange(double a, double b, double c) { 
     return b > a ? c >= a && c <= b : c >= b && c <= a; 
    } 

} 

도움을 주시면 감사하겠습니다.

답변

1

으로 이것을 추가합니다. 문제는 입력 필터 내부에서 구현 된 로직 때문이었습니다.

다음은 업데이트 된 코드 입력 필터입니다.

public static class InputFilterForDoubleMinMax implements InputFilter { 

     private double min, max; 

     public InputFilterForDoubleMinMax(double min, double max) { 
      this.min = min; 
      this.max = max; 
     } 

     public InputFilterForDoubleMinMax(String min, String max) { 
      this.min = Double.parseDouble(min); 
      this.max = Double.parseDouble(max); 
     } 

     @Override 
     public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { 
      try { 
       String temp; 
       if (dstart == 0) { 
        temp = source.toString(); 
       } else { 
        temp = dest.toString() + source.toString(); 
       } 
       if (source.toString().equalsIgnoreCase(".")) { 
        return ""; 
       } else if (temp.toString().indexOf(",") != -1) { 
        temp = temp.toString().substring(temp.toString().indexOf(",") + 1); 
        if (temp.length() > 2) { 
         return ""; 
        } 
       } 


       double input; 
       if (dstart == 0) { 
        input = Double.parseDouble(source.toString().replace(",", ".").replace("€", "")); 

       } else { 
        input = Double.parseDouble(dest.toString().replace(",", ".").replace("€", "") + source.toString().replace(",", ".").replace("€", "")); 
       } 

       if (isInRange(min, max, input)) 
        return null; 
      } catch (NumberFormatException nfe) { 
      } 
      return ""; 
     } 

     private boolean isInRange(double a, double b, double c) { 
      return b > a ? c >= a && c <= b : c >= b && c <= a; 
     } 

    } 
0

괜찮습니다.

package com.imrhk.testapp; 

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.text.Editable; 
import android.text.TextUtils; 
import android.text.TextWatcher; 
import android.view.View; 
import android.widget.EditText; 

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     final EditText editText = (EditText) findViewById(R.id.edit_text); 

     editText.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if(!TextUtils.isEmpty(editText.getText().toString())) { 
        editText.selectAll(); 
       } 
      } 
     }); 

     editText.addTextChangedListener(new TextWatcher() { 
      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
       if(editText.getSelectionStart() != -1) { 
        //editText.setText(""); 
        editText.setSelected(false); 
       } 
      } 

      @Override 
      public void onTextChanged(CharSequence s, int start, int before, int count) { 

      } 

      @Override 
      public void afterTextChanged(Editable s) { 

      } 
     }); 
    } 
} 

R.layout.activity_main

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <EditText 
     android:id="@+id/edit_text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:inputType="numberDecimal"/> 
</LinearLayout> 

편집 :

는 문제가 해결 될 때 필터

editText.setFilters(new InputFilter[]{new InputFilter() { 
      @Override 
      public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { 
       if(String.valueOf(source).equals(".") && String.valueOf(dest).equals("100")) //replace it with maxed value 
        return ""; 
       try { 
        Float f = Float.parseFloat(String.valueOf(dest) + String.valueOf(source.toString())); 
        if (f < 0.0f || f > 100.0f) //replace it with max/min value 
         return ""; 
        return null; 
       } catch (NumberFormatException e) { 
        return null; 
       } 
      } 
     }}); 
+0

확인 업데이트 된 질문. –

+0

'inputType = "numberDecimal"을 사용하면 범위 필터 만 사용하여 필터링해야합니다. –

+0

Doing samething –