アプリをアクティブ化するためのアクティビティに 2 つの EditText があります。1 つ目は名前を格納し、入力タイプを textCapWords に設定し、2 つ目はアクティベーション キーを格納し、入力タイプを textCapCharacters に設定します。
問題は、前の EditText を選択した後に最初の EditText を選択すると、capslock がオンのままになり、別のアクティビティに移動してもオンのままになることです。私がやりたいことは、独自のインプットメソッドを作成することなく、それをシフトダウンするか、capslock を完全にオフにすることです。インストルメンテーション オブジェクトを使用してシフト キーや capslock キーを送信しようとしましたが、これはハードウェア キーボードにのみ影響するはずです。これは簡単にできるはずだと思います。
以下はレイアウト ファイルの抜粋です。2.3.7 以前では問題なく動作します。4.0.4 では入力が変化していないようです。
<EditText
android:id="@+id/et_device_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="7dp"
android:hint="Please type in a unique name."
android:singleLine="true"
android:inputType="textCapWords"
/>
<EditText
android:id="@+id/et_activation_key"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:inputType="textCapCharacters"
android:maxLength="48"
/>
編集:私が持っている他の唯一のものは、それぞれの編集テキストのエラーを null に設定する 2 つの textchanged リスナーです。カウントが1の場合。したがって、機能には影響しません。
編集:また、最初の編集テキストで capslock を手動でオフにした場合、通常の入力タイプのルールが使用されます (前に文字がある場合は小文字で、新しい単語の場合はスペースにシフトします)。
編集:
et_DeviceName.addTextChangedListener( new TextWatcher()
{
@Override
public void onTextChanged( CharSequence s, int start, int before, int count )
{
if(count == 1)
{
et_DeviceName.setError( null );
}
}
@Override
public void beforeTextChanged( CharSequence s, int start, int count, int after )
{
}
@Override
public void afterTextChanged( Editable s )
{
}
}
);
et_ActivationKey.addTextChangedListener( new TextWatcher()
{
@Override
public void onTextChanged( CharSequence s, int start, int before, int count )
{
if( count == 1 )
{
et_ActivationKey.setError( null );
}
}
@Override
public void beforeTextChanged( CharSequence s, int start, int count, int after )
{
}
@Override
public void afterTextChanged( Editable s )
{
}
}
);
EDIT:私はソフトキーボードでの作業をあきらめ、テキストが入力されたときに EditText を手動で大文字にすることで問題を解決しました。以下は、同じ問題に遭遇した人のための私のコードです。
et_DeviceName = (EditText) findViewById( R.id.et_device_name );
et_DeviceName.setInputType( InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS );
et_DeviceName.setText( MainActivity.DEVICE_NAME );
//Move the cursor to the end of the text, for easy editing
Selection.setSelection( et_DeviceName.getEditableText(), et_DeviceName.getText().length() );
et_ActivationKey = (EditText) findViewById( R.id.et_activation_key );
//et_ActivationKey.setInputType( InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS );
btn_Activate = (Button) findViewById( R.id.btn_submit_act_key );
et_DeviceName.addTextChangedListener( new TextWatcher()
{
@Override
public void onTextChanged( CharSequence s, int start, int before, int count )
{
if(count == 1)
{
et_DeviceName.setError( null );
}
}
@Override
public void beforeTextChanged( CharSequence s, int start, int count, int after )
{
}
@Override
public void afterTextChanged( Editable s )
{
}
}
);
et_ActivationKey.addTextChangedListener( new TextWatcher()
{
boolean capitalise = true;
/* Exists because for some reason the text randomly goes lower case, when this happens the methods are called 3 times.
* So this tracks how many times the methods have been called and if called amount is already on two then it will re-capitalise.
*/
byte calledAmount = 0;
@Override
public void onTextChanged( CharSequence s, int start, int before, int count )
{
if( count == 1 )
{
et_ActivationKey.setError( null );
}
if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB && count != before)
{
capitalise = true;
}
}
@Override
public void beforeTextChanged( CharSequence s, int start, int count, int after )
{
}
@Override
public void afterTextChanged( Editable s )
{
if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB )
{
calledAmount ++;
if( capitalise || calledAmount == 2 )
{
capitalise = false;
calledAmount = 0;
s.replace( 0, s.length(), s.toString().toUpperCase() );
}
}
}
}
);
注: HoneyComb を確認する際に想定したのは、問題のバージョンが低いか高い (最も可能性が高い) 可能性があるということです。