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

希望这可以帮助别人