EditTextボックスがあり、それが数字キーパッドとして選択されたときに表示されるデフォルトのキーボードが必要です。ほとんどの場合、ユーザーは数字を入力するからです。ただし、必要に応じて、ユーザーがアルファベットも入力できるようにしたいのです。を使用android:inputType="number"
すると、入力が数字のみに制限されます。どのようなオプションがありますか?
19 に答える
xml で EditText を次のように定義します。
<EditText
android:id="@+id/txtOppCodigoCliente"
android:inputType="textVisiblePassword"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:maxLength="11"
android:hint="@string/strCodigoCliente"/>
出力: Android:inputType="textVisiblePassword"
数字の列と残りの文字。
より堅牢なソリューションをお勧めします。
入力タイプを選択する機能をユーザーに提示します。
必要な修正
-入力選択ボタンの表示/非表示の処理が改善されました
-入力タイプが文字の場合、おそらく提案を削除します
アクティビティは次のとおりです。
package mobi.sherif.numberstexttextview;
import android.os.Bundle;
import android.app.Activity;
import android.text.InputType;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.EditText;
public class MainActivity extends Activity {
EditText mEdit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mEdit = (EditText) findViewById(R.id.input);
mEdit.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean focused) {
findViewById(R.id.llmenu).setVisibility(focused?View.VISIBLE:View.GONE);
}
});
}
public void onNumbers(View v) {
mEdit.setInputType(InputType.TYPE_CLASS_NUMBER);
}
public void onLetters(View v) {
mEdit.setInputType(InputType.TYPE_CLASS_TEXT);
}
}
アクティビティの xml は次のとおりです。activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
android:id="@+id/scrollview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignWithParentIfMissing="true"
android:layout_above="@+id/llmenu" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number" >
<requestFocus />
</EditText>
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="@+id/llmenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#000000"
android:padding="0dp" >
<Button
android:id="@+id/buttonnumbers"
android:layout_width="0dp"
android:onClick="onNumbers"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Numbers" />
<Button
android:id="@+id/buttonletters"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="onLetters"
android:text="Letters" />
</LinearLayout>
</RelativeLayout>
これがあなたが探しているものです:
EditText
xmlで次のように定義します。
<EditText
android:id="@+id/etTest"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/hint">
</EditText>
そして、あなたのonCreate()
:で以下を呼び出します
etTest = (EditText) findViewById(R.id.etTest);
etTest.setRawInputType(InputType.TYPE_CLASS_NUMBER);
// Note the "Raw"
あなたがあなたの今をクリックするときはいつでも、あなたは数字と文字EditText
を入力することができます!(数字キーボードが最初に表示されます)
EditText に設定できる inputType は 1 つだけなので、残念ながらこれは不可能です。以下は、ボタンにアルファベット文字を表示するだけです。
<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text|number" />
ただし、入力タイプを数値に設定し、テンキーの近くのフォームに小さな switchKeyboard ボタンを追加することをお勧めします。そしてそのonClickを設定します
editText1.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
ユーザーがアルファベット文字に切り替えることもできます。
基本的に、EditText inputtype は、EditTextで入力タイプを設定するために使用されます。
の可能な値は次のandroid:inputtype
とおりです。
- 文章
- textCapCharacters
- textCapWords
- textCapSentences
- テキストオートコレクト
- テキストオートコンプリート
- テキスト複数行
- textImMultiLine
- textNoSuggestions
- textUri
- テキストメールアドレス
- textEmail件名
- textShortMessage
- textLongMessage
- textPersonName
- textPostalAddress
- テキストパスワード
- textVisiblePassword
- textWebEditText
- テキストフィルター
- テキストふりがな
- 番号
- 番号署名済み
- numberDecimal
- 電話
- 日付時刻
- 日にち
- 時間
次に、例に示すように任意の値を使用できます。
<EditText android:id="@+id/EditText01"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:inputType="number"
android:text="1212223"/>
Asus タブレットにはその機能があり、デフォルトのキーボードがカスタマイズされています。また、キーボードには数字とアルファベットのみが含まれています。あなたの要件の解決策は、デフォルトのキーボードをカスタマイズするには多すぎると思います
アクティビティの OnCreate で、次の操作を行います。
t = (EditText) findViewById(R.id.test);
t.setKeyListener(new KeyListener() {
@Override
public boolean onKeyUp(View view, Editable text, int keyCode, KeyEvent event) {
return false;
}
@Override
public boolean onKeyOther(View view, Editable text, KeyEvent event) {
return false;
}
@Override
public boolean onKeyDown(View view, Editable text, int keyCode, KeyEvent event) {
if (keyCode == 67) { // Handle backspace button =)
if (text.length() > 0) {
t.setText(text.subSequence(0, text.length() - 1));
t.setSelection(text.length()-1);
}
}
return false;
}
@Override
public int getInputType() {
return InputType.TYPE_CLASS_NUMBER;
}
@Override
public void clearMetaKeyState(View view, Editable content, int states) {
}
});
この場合、「 is 」number
と入力すると、Android は数字キーボードを表示しますが、好きなように入力できます。また、必要に応じて他のメソッドをオーバーライドすることもできます。
プログラム的には、通常の流れを少し調整するだけで可能です。まず、editText を次のように設定する必要があります。
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
次に、キーイベントをリッスンする必要があります。ポンドを押したら、InputType を再度 InputType.TYPE_CLASS_TEXT に設定します。これは私にとってはうまくいくはずです。
editText.setOnKeyListener(new View.OnKeyListener()
{
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
Log.d("KeyBoard", "Keyboard Test Key Hit");
switch (keyCode)
{
KeyEvent.KEYCODE_POUND:
if(editText.setInputType(InputType.TYPE_CLASS_TEXT);
{
editText.setInputType(InputType.TYPE_CLASS_TEXT);
return true;
}
}
}
}
あなたはParesh Mayaniの答えに従うことができます.
アドバイスではandroid:inputType="textPhonetic"
、ユーザーの選択に基づいて、inputType Text および Number に使用できます。
あなたが私の主張を理解してくれることを願っています。
次のいずれかのトグル ボタンを使用しないのはなぜですか。
- 数値のみを許可するように入力タイプを設定します
- 入力タイプを設定して、QWERTY キーボードを使用できるようにします
- または、他の入力タイプを設定します (必要な場合)
そうすれば、数値キーパッドをデフォルトに設定でき、ユーザーはボタンを押してそれを変更できます。
これは、さまざまな携帯電話の現在のデフォルトの Android 入力タイプを考えると不可能です。
一般的に回答に含まれる未加工の入力タイプを設定する一般的な方法のように、一部のキーボードでは機能するが他のキーボードでは機能しない可能性のあるハックがたくさんあります。
editText.setRawInputType(InputType.TYPE_CLASS_NUMBER)
私が見つけた最善のアドバイスはtextPostalAddress
、XML で入力タイプを使用することです。
android:inputType="textPostalAddress"
残念ながら、これはまだ私たちが望んでいることではありませんが、将来的にはそうなるでしょう。これの唯一の利点は、編集ボックスのメタ情報が意図したものを反映することです。
textView のコードで、android:inputType の「テキスト」を削除します。
そのはず、
<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="number" />
Saurav が言うように、最良の方法は、単純なリスナーを使用して QWERTY キーボードと数値キーボードを切り替えることです。
input.setInputType(InputType.TYPE_CLASS_TEXT);
input.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
switch (keyCode) {
case 24: // Volumen UP
input.setInputType(InputType.TYPE_CLASS_NUMBER);
break;
case 25: // Volumen DOWN
input.setInputType(InputType.TYPE_CLASS_TEXT);
break;
}
return true;
}
});
私はすべての方法を試しますが、完璧ではありません。だから私は別の考えを持っています。すべてのキーボードに最適です。
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reset_password);
editText.setRawInputType(InputType.TYPE_CLASS_NUMBER);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks whether a hardware keyboard is available
if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
Toast.makeText(this, "keyboard visible", Toast.LENGTH_SHORT).show();
if (editText.isFocused()) {
editText.setInputType(InputType.TYPE_CLASS_TEXT);
}
} else if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) {
Toast.makeText(this, "keyboard hidden", Toast.LENGTH_SHORT).show();
if (editText.getInputType() == InputType.TYPE_CLASS_TEXT) {
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
}
}
}
EditText の xml レイアウトに、次のパラメーターを追加します。
android:inputType="number|text"
これにより、ユーザーはテキストと数値の両方を入力でき、アクティビティやフラグメントにコードを含めるのではなく、xml にロジックを含めることができます。