如何在顶部使用数字打开Android键盘到AlphaNumeric?

时间:2014-11-10 19:55:04

标签: android

我正在尝试找到合适的输入法来让Android应用程序打开到键盘,其中数字位于顶部,alpha位于底部。我们输入的注册号通常以数字值开头,然后是一个alpha部分,因此如果首先打开数字就会很好。

我试过android:inputType =" Number"和android:inputType =" Text"

2 个答案:

答案 0 :(得分:1)

查看本教程: http://www.fampennings.nl/maarten/android/09keyboard/index.htm

它会逐步显示如何创建具有所需布局的自定义键盘。它可能不是最优雅的解决方案,但它可以让您完全控制,并允许您创建一个在所有设备上具有相同布局的键盘。

编辑:教程的简短摘要。

  1. 在“res”文件夹中创建名为“xml”的新文件夹。
  2. 在该文件夹中创建一个新的.xml文件,并将其命名为“alphanumkbd.xml”。该文件将包含键盘的布局。您可以查看Keyboard class documentation以供参考。
  3. 将以下内容粘贴到.xml文件中(从示例中复制):
  4. <?xml version="1.0" encoding="utf-8"?>
    <Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
          android:keyWidth="12.50%p"
          android:keyHeight="10%p" >
        <Row>
            <Key android:codes="55"    android:keyLabel="7" android:keyEdgeFlags="left" />
            <Key android:codes="56"    android:keyLabel="8" />
            <Key android:codes="57"    android:keyLabel="9" />
            <Key android:codes="65"    android:keyLabel="A" android:horizontalGap="6.25%p" />
            <Key android:codes="66"    android:keyLabel="B" />
            <Key android:codes="-5"    android:keyIcon="@android:drawable/ic_delete" android:isRepeatable="true" android:horizontalGap="6.25%p" />
            <Key android:codes="55006" android:keyLabel="CLR" android:keyEdgeFlags="right"/>
        </Row>
        <Row>
            <Key android:codes="52"    android:keyLabel="4" android:keyEdgeFlags="left"  />
            <Key android:codes="53"    android:keyLabel="5" />
            <Key android:codes="54"    android:keyLabel="6" />
            <Key android:codes="67"    android:keyLabel="C" android:horizontalGap="6.25%p" />
            <Key android:codes="68"    android:keyLabel="D" />
        </Row>
        <Row>
            <Key android:codes="49"    android:keyLabel="1"  android:keyEdgeFlags="left" />
            <Key android:codes="50"    android:keyLabel="2" />
            <Key android:codes="51"    android:keyLabel="3" />
            <Key android:codes="69"    android:keyLabel="E" android:horizontalGap="6.25%p" />
            <Key android:codes="70"    android:keyLabel="F" />
        </Row>
        <Row>
            <Key android:codes="48"    android:keyLabel="0" android:keyWidth="25%p" android:horizontalGap="6.25%p" android:keyEdgeFlags="left" />
            <Key android:codes="-3"    android:keyLabel="DONE" android:keyWidth="25%p" android:horizontalGap="12.50%p" />
            <Key android:codes="55000" android:keyLabel="PREV" android:horizontalGap="6.25%p" />
            <Key android:codes="55005" android:keyLabel="NEXT" android:keyEdgeFlags="right" />
        </Row>
    </Keyboard>
    
    1. 在您的活动中添加EditText和KeyboardView(再次从示例中复制):
    2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  xmlns:tools="http://schemas.android.com/tools"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent" >
      
          <EditText
              android:id="@+id/edittext0"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_alignParentTop="true"
              android:layout_centerHorizontal="true"
              android:inputType="text" />
      
          <android.inputmethodservice.KeyboardView
              android:id="@+id/keyboardview"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_alignParentBottom="true"
              android:layout_centerHorizontal="true"
              android:focusable="true"
              android:focusableInTouchMode="true"
              android:visibility="gone" />
      
      </RelativeLayout>
      
      1. 在onCreate()方法的活动中,使用以下代码初始化键盘:
      2. @Override protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        
            // Create the Keyboard
            Keyboard mKeyboard= new Keyboard(MainActivity.this ,R.xml.alphanumkbd);
        
            // Lookup the KeyboardView
        
            mKeyboardView = (KeyboardView) findViewById(R.id.keyboardview);
            // Attach the alphanumkbd to the view
            mKeyboardView.setKeyboard(mKeyboard);
        
            // Do not show the preview balloons
            mKeyboardView.setPreviewEnabled(false);
        
            // Install the key handler
            mKeyboardView.setOnKeyboardActionListener(mOnKeyboardActionListener);
        
            // Hide the standard keyboard initially
            getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        
            // Find the EditText
            EditText edittext= (EditText)findViewById(R.id.edittext0);
        
            //Make the custom keyboard appear
            edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override public void onFocusChange(View v, boolean hasFocus) {
                    if( hasFocus ) showCustomKeyboard(v); else hideCustomKeyboard();
                }
            });
        
            edittext.setOnClickListener(new View.OnClickListener() {
                @Override public void onClick(View v) {
                    showCustomKeyboard(v);
                }
            });
        
        
            edittext.setOnTouchListener(new View.OnTouchListener() {
                @Override public boolean onTouch(View v, MotionEvent event) {
                    EditText edittext = (EditText) v;
                    int inType = edittext.getInputType();       // Backup the input type
                    edittext.setInputType(InputType.TYPE_NULL); // Disable standard keyboard
                    edittext.onTouchEvent(event);               // Call native handler
                    edittext.setInputType(inType);              // Restore input type
                    return true; // Consume touch event
                }
            });
        }
        

        实用方法:

        public void hideCustomKeyboard() {
            mKeyboardView.setVisibility(View.GONE);
            mKeyboardView.setEnabled(false);
        }
        
        public void showCustomKeyboard( View v ) {
            mKeyboardView.setVisibility(View.VISIBLE);
            mKeyboardView.setEnabled(true);
            if( v!=null ) ((InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(v.getWindowToken(), 0);
        }
        
        public boolean isCustomKeyboardVisible() {
            return mKeyboardView.getVisibility() == View.VISIBLE;
        }
        

        变量 mOnKeyboardActionListener KeyboardView.OnKeyboardActionListener 的实现。您需要实现onKey()方法,例如:

        public final static int CodeDelete   = -5; // Keyboard.KEYCODE_DELETE
        public final static int CodeCancel   = -3; // Keyboard.KEYCODE_CANCEL
        public final static int CodeAllLeft  = 55001;
        public final static int CodeLeft     = 55002;
        public final static int CodeRight    = 55003;
        public final static int CodeAllRight = 55004;
        public final static int CodeClear    = 55006;
        
        private KeyboardView.OnKeyboardActionListener mOnKeyboardActionListener = new KeyboardView.OnKeyboardActionListener() {
            @Override public void onPress(int primaryCode) {
        
            }
        
            @Override public void onRelease(int primaryCode) {
        
            }
        
            @Override public void onKey(int primaryCode, int[] keyCodes) {
        
                // Get the EditText and its Editable
                View focusCurrent = MainActivity.this.getWindow().getCurrentFocus();
                if( focusCurrent==null || focusCurrent.getClass()!=EditText.class ) return;
                EditText edittext = (EditText) focusCurrent;
                Editable editable = edittext.getText();
                int start = edittext.getSelectionStart();
                // Handle key
                if( primaryCode==CodeCancel ) {
                    hideCustomKeyboard();
                } else if( primaryCode==CodeDelete ) {
                    if( editable!=null && start>0 ) editable.delete(start - 1, start);
                } else if( primaryCode==CodeClear ) {
                    if( editable!=null ) editable.clear();
                } else if( primaryCode==CodeLeft ) {
                    if( start>0 ) edittext.setSelection(start - 1);
                } else if( primaryCode==CodeRight ) {
                    if (start < edittext.length()) edittext.setSelection(start + 1);
                } else if( primaryCode==CodeAllLeft ) {
                    edittext.setSelection(0);
                } else if( primaryCode==CodeAllRight ) {
                    edittext.setSelection(edittext.length());
                } else {// Insert character
                    editable.insert(start, Character.toString((char) primaryCode));
                }
            }
        
            @Override public void onText(CharSequence text) {
        
            }
        
            @Override public void swipeLeft() {
        
            }
        
            @Override public void swipeRight() {
        
            }
        
            @Override public void swipeDown() {
        
            }
        
            @Override public void swipeUp() {
        
            }
        };
        

        正如您所注意到的,传递给 onKey()方法的第一个int参数对应于您为 android:codes 值> alphanumkbd.xml 即可。

        1. 实现onBackPressed()方法以隐藏自定义键盘(如果显示)。
        2. @Override public void onBackPressed() {
              if( isCustomKeyboardVisible() ) hideCustomKeyboard(); else this.finish();
          }
          

          这就是它的全部内容,这应该给你一个有效的例子。

答案 1 :(得分:0)

嘿,你所看到的是完全取决于设备的东西。这不能通过代码来实现。 在某些设备中,它将完全符合您的要求,而在其他用户中,必须单击键盘上的小编号键才能在数字和字符之间切换。