2013年1月22日

EditText Code


EditText Code

class EditTextAmount extends EditText implements OnClickListener ,OnKeyListener{
public EditTextAmount(Context context) {
super(context);
this.setId(9002);
this.setBackgroundColor(Color.argb(255, 255, 255, 255));
this.setText("99");
}

public void onClick(View arg0) {
// TODO Auto-generated method stub

}

public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
// TODO Auto-generated method stub
return false;
}
}


//設置密碼為不可見。
this.setTransformationMethod(PasswordTransformationMethod.getInstance());

//字數限制的設置

this.setFilters(new InputFilter[]{newInputFilter.LengthFilter(100)});

//調用數字鍵盤

this.setInputType(InputType.TYPE_CLASS_NUMBER);

//設置輸入類型和鍵盤為英文

this.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);

//監聽事件enter
輸入完成

editTextA.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        //actionId == XX_SEND 或者 XX_DONE時都觸發        //或者event.getKeyCode == ENTER  event.getAction == ACTION_DOWN時也觸發        //注意,這是一定要判斷event != null。因為在某些輸入法上會返回null        if (actionId == EditorInfo.IME_ACTION_SEND                || actionId == EditorInfo.IME_ACTION_DONE                || (event != null && KeyEvent.KEYCODE_ENTER == event.getKeyCode() && KeyEvent.ACTION_DOWN == event.getAction())) {
            //處理事件            if (spinnerA != null) {
                init(spinnerA.getSelectedItemPosition());
                myAdapter.notifyDataSetChanged();
            }
        }
        return false;
    }
});

//監聽事件
輸入文字變更
editTextA.addTextChangedListener(new TextWatcher() {
            @Override            public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub                System.out.println("afterTextChanged || 按下後");

            }

            @Override            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
// TODO Auto-generated method stub                System.out.println("beforeTextChanged || 按下前");
            }

            @Override            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
// TODO Auto-generated method stub                System.out.println("onTextChanged || 按下中");
            }
        });

沒有留言:

張貼留言