2015-02-24 59 views
2

我爲我的一個活動創建了一個佈局,用戶可以在其中插入某個EditText窗口小部件的值。我需要這些EditText中的某些必須具有不可編輯的後綴(如cm,mm等)。 用戶插入值後,我將解析這些EditText的內容,避免後綴,因此我將處理沒有後綴的唯一輸入。怎麼做?帶有不可編輯/不可取消後綴的EditText

我已經在這裏搜索和搜索過,但沒有任何幫助。 我找到了這樣一個https://stackoverflow.com/a/20794581/2516399這樣的答案,幫不了我。

我希望我是清楚我的問題...對不起,我的英語

回答

3

試試這個

+0

看來解決辦法,但我忘了說,我有這個在我的EditText:'.setFilters(新輸入過濾器[] {新DecimalDigitsInputFilter(2) });'讓用戶只用2位插入小數。在這種情況下如何使它工作?無論如何,我使用'.endsWith'而不是'.startWith',然後'.setSelection(0);'因爲它是後綴,而不是前綴 – smartmouse 2015-02-24 22:20:03

+0

你不能設置'android:inputType =「number」'和'android:maxLength = 「xml」中的「2」,然後將前綴放在'.class'中,就像我在回答中寫的一樣? – Apurva 2015-02-25 04:03:12

+0

在我的XML文件中,我只有'android:inputType =「numberDecimal」',並在.class文件中編寫了這個:http://pastebin.com/b442zUt5 正如我所說如果我刪除'.setFilters(new InputFilter [ ] {新的DecimalDigitsInputFilter(2)});'它的工作;如果我保留這段代碼,它不會讓我在EditText中寫入輸入,除了已經存在的後綴。 – smartmouse 2015-02-25 22:23:17

2
private static final String mSuffix = "SUFX"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     final EditText et = new EditText(this); 
     et.setText(mSuffix); 

     et.setOnFocusChangeListener(new OnFocusChangeListener() { 
      @Override 
      public void onFocusChange(View v, boolean hasFocus) { 

       //NO FOCUS 
       if(!hasFocus){ 
        //HAS USER CLEARED THE SUFFIX 
        if(!et.getText().toString().contains(mSuffix)){ 
         //ADDING SUFFIX AGAIN 
         String newText = et.getText().toString(); 
         et.setText(mSuffix+newText); 
        } 
       } 
      } 
     }); 


    } 

在我看來,使用正進出口。檢查後綴。它會更安全和簡單。

SOLUTION:2

下面是另一種解決方案,一些棘手)

<RelativeLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 
    <EditText 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/test_value" 
      android:layout_alignParentLeft="true" 
      android:id="@+id/editText2" 
      android:layout_gravity="center" 
      /> 
    <TextView 
      android:id="@+id/text_hint" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_centerVertical="true" 
      android:text="@string/hint_test" 
      android:textSize="18sp" 
      android:layout_marginRight="10dp" 
      android:textColor="#808080" 
      /> 
</RelativeLayout> 

RESULT/OUTPUT

OUTPUT:

REF:計算器

+0

看來我不能用它:' - OnFocusChangeListener不能被解析爲一個類型 \t - 在類型視圖的方法setOnFocusChangeListener(View.OnFocusChangeListener)不適用於參數(新OnFocusChangeListener() \t {})' – smartmouse 2015-02-24 22:23:21

+1

嘗試(新的View.OnFocusChangeListener){} ... – theapache64 2015-02-25 02:22:46

+0

它的工作原理,現在我看到了OnFocusChange做什麼......但它不是我正在尋找。無論如何,我真的很感激你的回覆,我會用它來達到其他目的! 請看看Apurva的答案,並試圖找出如何讓他的解決方案在我的案例中工作。謝謝 – smartmouse 2015-02-25 22:32:30

5

這是我的解決方案:在文本後面繪製後綴的EditText類。有兩個自定義屬性用於定義後綴和後綴填充的文本(位於EditText的左側)。

public class EditTextWithSuffix extends EditText { 
    TextPaint textPaint = new TextPaint(); 
    private String suffix = ""; 
    private float suffixPadding; 

    public EditTextWithSuffix(Context context) { 
     super(context); 
    } 

    public EditTextWithSuffix(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     getAttributes(context, attrs, 0); 
    } 

    public EditTextWithSuffix(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
     getAttributes(context, attrs, defStyleAttr); 
    } 

    @Override 
    public void onDraw(Canvas c){ 
     super.onDraw(c); 
     int suffixXPosition = (int) textPaint.measureText(getText().toString()) + getPaddingLeft(); 
     c.drawText(suffix, Math.max(suffixXPosition, suffixPadding), getBaseline(), textPaint); 
    } 

    @Override 
    protected void onFinishInflate() { 
     super.onFinishInflate(); 
     textPaint.setColor(getCurrentTextColor()); 
     textPaint.setTextSize(getTextSize()); 
     textPaint.setTextAlign(Paint.Align.LEFT); 
    } 

    private void getAttributes(Context context, AttributeSet attrs, int defStyleAttr) { 
     TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.EditTextWithSuffix, defStyleAttr, 0); 
     if(a != null) { 
      suffix = a.getString(R.styleable.EditTextWithSuffix_suffix); 
      if(suffix == null) { 
       suffix = ""; 
      } 
      suffixPadding = a.getDimension(R.styleable.EditTextWithSuffix_suffixPadding, 0); 
     } 
     a.recycle(); 
    } 
} 

這裏的屬性定義:

<resources> 
    <declare-styleable name="EditTextWithSuffix"> 
     <attr name="suffix" format="string|reference" /> 
     <attr name="suffixPadding" format="dimension" /> 
    </declare-styleable> 
</resources>