0

我正在爲nexus 7開發一個應用程序,並且我需要某些EditText來顯示帶有數字和特殊字符的鍵盤視圖。我知道你可以使用inputType爲EditText設置佈局,但是我的問題是,如果我設置了inputType =「number」,撥號盤出現並且不可能切換到字符視圖。我所需要的(是客戶的要求)是當您點擊左下角的「123」鍵時,顯示的佈局打開鍵盤。 我試過setRawInputType和setInputType的所有組合,沒有運氣。 這種組合顯示出撥號盤android默認鍵盤佈局

txtLineCode.setInputType(InputType.TYPE_CLASS_TEXT); 
txtLineCode.setRawInputType(InputType.TYPE_CLASS_NUMBER); 

這個組合顯示出撥號盤太

txtLineCode.setInputType(InputType.TYPE_CLASS_TEXT); 
txtLineCode.setRawInputType(InputType.TYPE_NUMBER_VARIATION_NORMAL|InputType.TYPE_CLASS_NUMBER); 

下面是截圖,以便更好地解釋什麼,我需要

這是默認的鍵盤

this is the default keyboard

這是當我點擊「?123」中所示的佈局,這是我需要的默認 this is the layout shown when I click on "?123"

顯示出這個佈局,如果我設置的inputType =「號」,它不允許切換到佈局設計 enter image description here

現在我的一些EditText普遍是數字,但是應該包含數字,我該怎麼辦?

千恩萬謝

+0

請記住,如果你得到這個工作(我懷疑),用戶可以安裝具有不同的鍵盤完全不同的行爲。 – Siebe

+0

是的,我知道,但我的目標是確保股票鍵盤的正確行爲。你爲什麼懷疑讓它工作? – Apperside

+0

如果你想讓它顯示123下的鍵,那麼停止用戶切換回來的是什麼。嘗試自定義鍵盤http://stackoverflow.com/questions/9577304/how-to-make-a-android-custom-keyboard –

回答

1

我想我發現了一個很優雅的解決方案: 我用在文本框中可抽拉(在我的情況drawableRight),我已經分配了一個點擊監聽,剛上繪製執行數字和文本模式之間的切換。 我能夠利用Handling click events on a drawable within an EditText採取小動作分配聽衆剛上繪製:

public class MyEdittextextends EditText { 

    private Drawable drawableRight; 
    private Drawable drawableLeft; 
    private Drawable drawableTop; 
    private Drawable drawableBottom; 


//YOUR STUFF HERE 

@Override 
    public void setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom) { 
     if (right != null) { 
      drawableRight = right; 
     } 

     if (left != null) { 
      drawableLeft = left; 
     } 
     super.setCompoundDrawables(left, top, right, bottom); 
    } 
View.OnClickListener _leftDrawableClickListener = null; 
View.OnClickListener _rightDrawableClickListener = null; 

public void setLeftDrawableClickListener(View.OnClickListener clickListener) { 
    _leftDrawableClickListener = clickListener; 
} 

public void setRightDrawableClickListener(View.OnClickListener clickListener) { 
    _rightDrawableClickListener = clickListener; 
} 

@Override 
    public boolean onTouchEvent(MotionEvent event) { 

     if (event.getAction() == MotionEvent.ACTION_DOWN) { 
      int x, y; 
      Rect bounds; 
      x = (int) event.getX(); 
      y = (int) event.getY(); 
      // this works for left since container shares 0,0 origin with bounds 
      if (drawableLeft != null) { 
       bounds = drawableLeft.getBounds(); 
       if (bounds.contains(x - fuzz, y - fuzz)) { 
        try { 
         _leftDrawableClickListener.onClick(this); 
        } catch (Exception e) { 
        } 
        if (consumeEvent) { 
         event.setAction(MotionEvent.ACTION_CANCEL); 
         return false; 
        } 
       } 
      } else if (drawableRight != null) { 
       bounds = drawableRight.getBounds(); 
       if (x >= (this.getRight() - bounds.width() - fuzz) && x <= (this.getRight() - this.getPaddingRight() + fuzz) && y >= (this.getPaddingTop() - fuzz) && y <= (this.getHeight() - this.getPaddingBottom()) + fuzz) { 

        try { 
         _rightDrawableClickListener.onClick(this); 
        } catch (Exception e) { 
        } 
        if (consumeEvent) { 
         event.setAction(MotionEvent.ACTION_CANCEL); 
         return false; 
        } 
       } 
      } else if (drawableTop != null) { 
       // not implemented yet 
      } else if (drawableBottom != null) { 
       // not implemented yet 
      } 
     } 

     return super.onTouchEvent(event); 
    } 


@Override 
    protected void finalize() throws Throwable { 
     drawableRight = null; 
     drawableBottom = null; 
     drawableLeft = null; 
     drawableTop = null; 
     super.finalize(); 
    } 

} 

一旦EditText上已創建的自定義,我在活動中使用此代碼

myEdittext = (EditText) findViewById(R.id.myEdittext); 
     myEdittext.setRightDrawableClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       if (myEdittext.getInputType() != InputType.TYPE_CLASS_TEXT) { 
        myEdittext.setInputType(InputType.TYPE_CLASS_TEXT); 
        myEdittext.setRawInputType(InputType.TYPE_CLASS_TEXT); 
        myEdittext.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.keyboard_123, 0); 

       } else { 
        myEdittext.setRawInputType(InputType.TYPE_CLASS_NUMBER); 
        myEdittext.setInputType(InputType.TYPE_CLASS_NUMBER); 
        myEdittext.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.keyboard_abc, 0); 
       } 

      } 
     }); 

這是結果:當首次示出的顯示的EditText這樣

enter image description here

並點擊「ABC」的形象變成這樣

enter image description here

希望這可以幫助別人