Android EditText自定義按鍵盤

效果圖:

主要問題點:

1.如何在點擊edittext時不讓系統的默認鍵盤顯示出來?

2.如何在點擊button後把數字傳入到edittext中?

 

1.如何在點擊edittext時不讓系統的默認鍵盤顯示出來

   網上有很多種方法,自己試過之後都不行,後面在Android源碼中找到兩種方法;

   a.重寫edittext 中ontouch方法,直接屏蔽掉MotionEvent.ACTION_UP

 if (event.getActionMasked() == MotionEvent.ACTION_UP) {
          cancelLongPress();
     }

   b.重寫edittext中的ontouch方法,手動的把按鍵盤關掉:

    public boolean onTouchEvent(MotionEvent event) {
        final boolean ret =super.onTouchEvent(event);
       final InputMethodManager imm = ((InputMethodManager) getContext()
               .getSystemService(Context.INPUT_METHOD_SERVICE));
       if (imm != null && imm.isActive(this)) {
           imm.hideSoftInputFromWindow(getApplicationWindowToken(), 0);
       }
       return ret; 
    }

 

2.如何在點擊button後把數字傳入到edittext中

 這個相對比較簡單,在button的onclick方法中,把相應的字符插入到edittext中:

	public void onClick(View view) {
		// TODO Auto-generated method stub
		if (view instanceof Button) {
        String text = ((Button) view).getText().toString();	
		int cursor=mEditText.getSelectionStart();
		mEditText.getText().insert(cursor,text);	
		}		
		}


源碼下載:http://download.csdn.net/detail/txj8612/7215135

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章