gpt4 book ai didi

Android - 更改屏幕键盘切换状态

转载 作者:行者123 更新时间:2023-11-30 03:39:27 25 4
gpt4 key购买 nike

我在激活我的应用程序的 Activity 中有两个 EditText。第一个存储名称并将输入类型设置为 textCapWords,第二个存储激活 key 并将输入类型设置为 textCapCharacters。

问题是,当您在选择前一个 EditText 之后选择第一个 EditText 时,大写锁定仍然打开并且即使您移动到另一个 Activity 也是如此。我想做的是,无需创建自己的输入法,将其向下移动到移位,甚至完全关闭大写锁定。我试过使用一个检测对象来发送一个 shift 键甚至一个 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"
/>

编辑:我仅有的另一件事是两个文本更改的监听器,它们都将各自的编辑文本的错误设置为空。如果计数为 1。那么它们不会影响功能。

编辑:哦,如果第一次编辑文本时,如果您手动关闭大写锁定,它会使用正常的输入类型规则(如果前面有一个字母则小写,并在空格上移动一个新词)。

编辑:

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 )
{
}
}
);

编辑:我已经放弃使用软键盘并通过在输入文本时手动将 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 时做了一个假设,问题所在的版本可能更低或更高(最有可能更高)。

最佳答案

尝试使用:

InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS;
InputType.TYPE_TEXT_FLAG_CAP_WORDS;
InputType.TYPE_TEXT_FLAG_CAP_SENTENCES;

示例:example of inputType或信息:information about inputType

编辑:这是在 stackoverflow 中对此的讨论 Programmatically change input type of the EditText from PASSWORD to NORMAL & vice versa

编辑:

EditText et1 = (EditText)findViewById(R.yourLayout.et1);
et1.setInputType(InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS); // capitalize all characters

EditText et2 = (EditText)findViewById(R.yourLayout.et2);
et2.setInputType(InputType.TYPE_TEXT_FLAG_CAP_SENTENCES); // capitalize the first
// letter of each sentence

编辑 2:

EditText et1 = (EditText)findViewById(R.yourLayout.et1);
et1.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_FLAG_CAP_CHARACTERS);

EditText et2 = (EditText)findViewById(R.yourLayout.et2);
et2.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);

关于Android - 更改屏幕键盘切换状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16037400/

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