【问题标题】:Android Capitalize characters not working in phoneAndroid大写字符在手机中不起作用
【发布时间】:2013-03-07 16:50:56
【问题描述】:

在我的应用中,我想让用户只输入大写字母。所以我使用大写属性。

这是代码的一部分

<EditText
    android:id="@+id/et5"
    android:layout_width="140dp"
    android:layout_height="wrap_content"
    android:layout_x="127dp"
    android:layout_y="219dp"
    android:maxLength="25"
    android:ems="10"
    android:singleLine="true"
    android:capitalize="characters"
    android:text="" />

在某些情况下我也试过这个

android:inputType="textCapCharacters"

在模拟器中可以正常使用,但在手机中无法使用。在电话中,它显示小写字母。这里的错误是什么以及如何克服?

【问题讨论】:

    标签: android android-layout android-edittext capitalize


    【解决方案1】:

    尝试像这样在 java 类中使用它:

    textView.setText(text.toUpperCase());
    

    您还可以使用 Textwatcher 方法对更改的文本应用大写

    要让它在 xml 中工作,你应该试试这个:

    android:inputType="textCapCharacters" 
    

    【讨论】:

    • 好的,然后尝试 android:inputType="textCapCharacters" 因为大写已被弃用
    • textCapSentences 只大写第一个字母
    • android:inputType="textCapCharacters" 。如果我的回答是正确的,请接受并投票赞成我的回答
    【解决方案2】:
    edittext.addTextChangedListener(new TextWatcher() {
    
    @Override
    public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {            
    
    }
        @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                    int arg3) {             
    }
    @Override
    public void afterTextChanged(Editable arg0) {
          String s=arg0.toString();
      if(!s.equals(s.toUpperCase()))
      {
         s=s.toUpperCase();
         edittext.setText(s);
      }
    }
    });     
    

    【讨论】:

    • 我已经提到了。我只想显示大写字母键盘
    【解决方案3】:

    试试这个

    <EditText
    android:inputType="textCapCharacters" />
    
    
    <EditText
    android:inputType="textCapWords" />
    

    或者你可以在java文件中以编程方式进行

     String str = et.getText.toString().toUpperCase();
    

    【讨论】:

      【解决方案4】:

      试试这个:

      editText.setFilters(new InputFilter[] {new InputFilter.AllCaps()});
      

      并将 EditText xml 属性设置为:

      android:inputType="text|textCapCharacters"
      

      希望对你有帮助:)

      【讨论】:

        猜你喜欢
        • 2012-08-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-26
        • 2014-05-02
        • 1970-01-01
        相关资源
        最近更新 更多