1

我有一个 EditText,对于用户在此 editText 中键入的每个字母,我想在 EditText 中的整个文本上应用一个函数,该函数是因为我用阿拉伯语写作,而阿拉伯语字母看起来不好,但是当我使用Farsi class它时变得很好,我会给你一个简单的例子,如果用户想输入 21,我想应用函数output=3*editText.getText(); ,所以当他输入 1 时,我将应用这个函数,所以会出现在 editText 上的结果是:

output = 3*1 = 3

当他输入 2 时,我想显示12*3 = 36

编辑

06-30 12:30:38.547: E/AndroidRuntime(276): FATAL EXCEPTION: main
06-30 12:30:38.547: E/AndroidRuntime(276): java.lang.StackOverflowError
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.text.DynamicLayout.reflow(DynamicLayout.java:261)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.text.DynamicLayout.<init>(DynamicLayout.java:150)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.widget.TextView.makeNewLayout(TextView.java:4851)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.widget.TextView.checkForRelayout(TextView.java:5348)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.widget.TextView.setText(TextView.java:2688)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.widget.TextView.setText(TextView.java:2556)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.widget.EditText.setText(EditText.java:75)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.widget.TextView.setText(TextView.java:2531)
06-30 12:30:38.547: E/AndroidRuntime(276):  at com.localizedbasedcompetition.AnswerQuestion$3.onTextChanged(AnswerQuestion.java:149)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.widget.TextView.setText(TextView.java:2691)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.widget.TextView.setText(TextView.java:2556)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.widget.EditText.setText(EditText.java:75)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.widget.TextView.setText(TextView.java:2531)
06-30 12:30:38.547: E/AndroidRuntime(276):  at com.localizedbasedcompetition.AnswerQuestion$3.onTextChanged(AnswerQuestion.java:149)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.widget.TextView.setText(TextView.java:2691)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.widget.TextView.setText(TextView.java:2556)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.widget.EditText.setText(EditText.java:75)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.widget.TextView.setText(TextView.java:2531)
06-30 12:30:38.547: E/AndroidRuntime(276):  at com.localizedbasedcompetition.AnswerQuestion$3.onTextChanged(AnswerQuestion.java:149)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.widget.TextView.setText(TextView.java:2691)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.widget.TextView.setText(TextView.java:2556)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.widget.EditText.setText(EditText.java:75)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.widget.TextView.setText(TextView.java:2531)
06-30 12:30:38.547: E/AndroidRuntime(276):  at com.localizedbasedcompetition.AnswerQuestion$3.onTextChanged(AnswerQuestion.java:149)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.widget.TextView.setText(TextView.java:2691)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.widget.TextView.setText(TextView.java:2556)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.widget.EditText.setText(EditText.java:75)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.widget.TextView.setText(TextView.java:2531)
06-30 12:30:38.547: E/AndroidRuntime(276):  at com.localizedbasedcompetition.AnswerQuestion$3.onTextChanged(AnswerQuestion.java:149)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.widget.TextView.setText(TextView.java:2691)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.widget.TextView.setText(TextView.java:2556)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.widget.EditText.setText(EditText.java:75)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.widget.TextView.setText(TextView.java:2531)
06-30 12:30:38.547: E/AndroidRuntime(276):  at com.localizedbasedcompetition.AnswerQuestion$3.onTextChanged(AnswerQuestion.java:149)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.widget.TextView.setText(TextView.java:2691)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.widget.TextView.setText(TextView.java:2556)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.widget.EditText.setText(EditText.java:75)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.widget.TextView.setText(TextView.java:2531)
06-30 12:30:38.547: E/AndroidRuntime(276):  at com.localizedbasedcompetition.AnswerQuestion$3.onTextChanged(AnswerQuestion.java:149)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.widget.TextView.setText(TextView.java:2691)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.widget.TextView.setText(TextView.java:2556)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.widget.EditText.setText(EditText.java:75)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.widget.TextView.setText(TextView.java:2531)
06-30 12:30:38.547: E/AndroidRuntime(276):  at com.localizedbasedcompetition.AnswerQuestion$3.onTextChanged(AnswerQuestion.java:149)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.widget.TextView.setText(TextView.java:2691)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.widget.TextView.setText(TextView.java:2556)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.widget.EditText.setText(EditText.java:75)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.widget.TextView.setText(TextView.java:2531)
06-30 12:30:38.547: E/AndroidRuntime(276):  at com.localizedbasedcompetition.AnswerQuestion$3.onTextChanged(AnswerQuestion.java:149)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.widget.TextView.setText(TextView.java:2691)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.widget.TextView.setText(TextView.java:2556)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.widget.EditText.setText(EditText.java:75)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.widget.TextView.setText(TextView.java:2531)
06-30 12:30:38.547: E/AndroidRuntime(276):  at com.localizedbasedcompetition.AnswerQuestion$3.onTextChanged(AnswerQuestion.java:149)
06-30 12:30:38.547: E/AndroidRuntime(276):  at android.widget.TextView.sendOnTextChang
4

4 回答 4

3

您必须像这样使用addTextChangedListener()TextWatcher

tv = (TextView) findViewById(R.id.tv);
textMessage = (EditText) findViewById(R.id.tvMessage);
textMessage.addTextChangedListener(new TextWatcher(){
    public void afterTextChanged(Editable s) {
        //do your function here
    }
    public void beforeTextChanged(CharSequence s, int start, int count, int after){}
    public void onTextChanged(CharSequence s, int start, int before, int count){}
}); 
于 2012-06-30T14:08:11.527 回答
2
EditText editText = (EditText)findViewById(R.id.editText);
    editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
        }
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            // TODO Auto-generated method stub
        }
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        try
        {
         String s1 = editText.Text.ToString();
         byte[] b = s.getBytes("UTF-8");
         String s2 = new String(b, "UTF-8");
          editText.Text=(s2);
        }
        catch(Exception e)
        {}
        } 
    });
于 2012-06-30T14:10:00.790 回答
1
fileNameEditText.setOnKeyListener(new View.OnKeyListener() {
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        return false;
    }
});

为您的视图设置一个关键的侦听器。

于 2012-06-30T14:07:32.933 回答
1

检查这个:TextView.OnEditorActionListener editText.setOnEditorActionListener(yourListener)

编辑:哦,我正在快速回答。当然,告诉使用 TextWatcher 的答案是正确的方法。

于 2012-06-30T14:10:26.723 回答