gpt4 book ai didi

android - 如何将 Android 键盘打开到顶部有数字的 AlphaNumeric?

转载 作者:行者123 更新时间:2023-11-29 01:37:46 27 4
gpt4 key购买 nike

我正在尝试找到合适的输入法来让 Android 应用程序打开键盘,数字在顶部,字母在底部。我们输入的注册号通常以数字值开头,但随后是字母部分,因此如果能先打开数字就更好了。

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

最佳答案

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

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

编辑:教程的简短摘要。

  1. 在“res”文件夹中创建一个名为“xml”的新文件夹。
  2. 在该文件夹中创建一个新的 .xml 文件,将其命名为例如“alphanumkbd.xml”。该文件将包含键盘布局。你可以查看Keyboard class documentation供引用。
  3. 将以下内容粘贴到 .xml 文件中(从示例中复制):
<?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. 在您的 Activity 中添加一个 EditText 和 KeyboardView(同样从示例中复制):
<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() 方法的 Activity 中使用以下代码初始化键盘:
@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;
}

变量mOnKeyboardActionListenerKeyboardView.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() 方法以隐藏显示的自定义键盘。
@Override public void onBackPressed() {
if( isCustomKeyboardVisible() ) hideCustomKeyboard(); else this.finish();
}

这就是它的全部内容,这应该可以为您提供一个有效的示例。

关于android - 如何将 Android 键盘打开到顶部有数字的 AlphaNumeric?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26851909/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com