Android - 更改屏幕键盘移位状态

时间:2013-04-16 12:44:08

标签: android android-softkeyboard

我在活动中有两个EditTexts用于激活我的应用。第一个存储名称,输入类型设置为textCapWords,第二个存储激活密钥,输入类型设置为textCapCharacters。

问题在于,当您在上一个之后选择第一个EditText时,即使您移动到另一个活动,大写锁定仍然打开并且处于打开状态。我想做的是,无需创建自己的输入法,就是将其向下移动,甚至完全关闭大写锁定。我尝试使用检测对象来发送shift键甚至是大写锁定密钥,但这只能影响硬件键盘。我认为这应该很容易做到。

这是布局文件的摘录,它在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"
 />

编辑:我唯一的另一件事是两个textchanged侦听器,它们都将各自的编辑文本错误设置为null。如果计数为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时做了一个假设,版本问题可能是更低或更高(更高的可能性)。

1 个答案:

答案 0 :(得分:1)

尝试使用:

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

<强> EDIT2:

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