1

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

问题是,当您在上一个之后选择第一个 EditText 时,即使您移动到另一个活动,大写锁定仍处于打开状态。我想做的,不必创建自己的输入法,将其向下移动到移位,甚至完全关闭大写锁定。我尝试过使用检测对象来发送 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"
 />

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

4

1 回答 1

1

尝试使用:

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

示例:输入类型或信息的示例:有关输入类型的信息

编辑:here is an discussion about this in stackoverflow Programmatically change input type of the EditText from PASSWORD to NORMAL &反之亦然

编辑:

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);
于 2013-04-16T13:25:46.210 回答