在AutoCompleteTextView中大写字母

时间:2014-02-07 13:30:50

标签: android autocompletetextview

当用户开始在AutoCompleteTextView中输入内容时如何大写所有字母?

我试过这种方式:

txtProductInput.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                String upper = txtProductInput.getText().toString().toUpperCase();
                txtProductInput.setText(upper);

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

            }
        });

但是当用户开始在线打印应用程序时

txtProductInput.setText(upper);

堆栈跟踪日志:

02-07 13:17:31.708: E/AndroidRuntime(12611): FATAL EXCEPTION: main
02-07 13:17:31.708: E/AndroidRuntime(12611): java.lang.StackOverflowError
02-07 13:17:31.708: E/AndroidRuntime(12611):    at android.text.method.ReplacementTransformationMethod$ReplacementCharSequence.getChars(ReplacementTransformationMethod.java:151)
02-07 13:17:31.708: E/AndroidRuntime(12611):    at android.text.TextUtils.getChars(TextUtils.java:70)
02-07 13:17:31.708: E/AndroidRuntime(12611):    at android.text.TextUtils.indexOf(TextUtils.java:103)
02-07 13:17:31.708: E/AndroidRuntime(12611):    at android.text.StaticLayout.generate(StaticLayout.java:182)
02-07 13:17:31.708: E/AndroidRuntime(12611):    at android.text.DynamicLayout.reflow(DynamicLayout.java:284)
02-07 13:17:31.708: E/AndroidRuntime(12611):    at android.text.DynamicLayout.<init>(DynamicLayout.java:170)
02-07 13:17:31.708: E/AndroidRuntime(12611):    at android.widget.TextView.makeSingleLayout(TextView.java:5843)
02-07 13:17:31.708: E/AndroidRuntime(12611):    at android.widget.TextView.makeNewLayout(TextView.java:5741)
02-07 13:17:31.708: E/AndroidRuntime(12611):    at android.widget.TextView.checkForRelayout(TextView.java:6280)
02-07 13:17:31.708: E/AndroidRuntime(12611):    at android.widget.TextView.setText(TextView.java:3547)
02-07 13:17:31.708: E/AndroidRuntime(12611):    at android.widget.TextView.setText(TextView.java:3405)
02-07 13:17:31.708: E/AndroidRuntime(12611):    at android.widget.EditText.setText(EditText.java:80)
02-07 13:17:31.708: E/AndroidRuntime(12611):    at android.widget.TextView.setText(TextView.java:3380)
02-07 13:17:31.708: E/AndroidRuntime(12611):    at com.DEM.productionmonitor.LineProduct$1.onTextChanged(LineProduct.java:99)

5 个答案:

答案 0 :(得分:2)

'ZanoOnStack'提出了一个好主意,所以如果其他人需要,这是一个合适的解决方案:

InputFilter[] FilterArray = new InputFilter[1];
FilterArray[0] = new InputFilter.AllCaps();
txtProductInput.setFilters(FilterArray);

通过这种方式,可以添加更多其他类型的过滤器。

答案 1 :(得分:1)

您是否尝试过xml布局?

android:textAllCaps="true"

答案 2 :(得分:1)

<AutoCompleteTextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:inputType="textCapCharacters"
/>

答案 3 :(得分:0)

尝试插入

String upper = s.getText().toString().toUpperCase();
s.setText(upper);

在afterTextChanged

答案 4 :(得分:0)

它对我有用

@Override
public void afterTextChanged(Editable arg) {
    String s=arg.toString();
    if(!s.equals(s.toUpperCase())){
        s=s.toUpperCase(); 
        tv.setText(s);
        tv.setSelection(tv.getText().length());

    }

}