【问题标题】:EditText Character Countdown IssueEditText 字符倒计时问题
【发布时间】:2014-10-08 07:01:35
【问题描述】:

我已经构建了一个计数器标签 TextView,它对 EditText 中剩余的字符数进行倒计时。我在 EditText 中允许 196 个字符。当 EditText 字符数达到零时,我将计数器标签的文本颜色更改为红色,并禁用 EditText,因此用户无法输入更多字符。

当我尝试删除输入的最后一个字符以启用用户输入时,我的问题出现了。我想要做的是删除最后一个字符,以便禁用 EditText 的逻辑不再正确,EditText 变为启用,计数器标签的文本颜色变回白色,并且计数被更新。但是,我编写的代码不起作用,并且 EditText 保持禁用状态。我正在使用 addTextChangedListener 来禁用和 onTouchListener 来尝试并重新启用。

我已经设法使用 TOUCHESBEGAN 方法在 iOS 上进行这项工作,该方法捕获用户的触摸,启用 UITextView,使用 DELETEBACKWARD 函数删除最后一个字符,并更新计数。有人可以研究我的代码并为我指明 Android 的正确方向吗?

public class MessageInputView extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_message_input_view);

    final ImageView continueButton = (ImageView) findViewById(R.id.continueButton);
    final ImageView backButton = (ImageView) findViewById(R.id.backButton);

    final EditText editText = (EditText) findViewById(R.id.editText);
    final TextView counterLabel = (TextView) findViewById(R.id.counterLabel);
    final int characterMax = 196;

    final MediaPlayer mediaPlayer = (MediaPlayer.create(MessageInputView.this, R.raw.droplet_sound));

    editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {

        }

        @Override
        public void afterTextChanged(Editable editable) {

            counterLabel.setText(characterMax - editable.toString().length() + " characters remaining");


            if (editText.length() == characterMax){

                counterLabel.setTextColor(Color.parseColor("#FF0000"));
                editText.setEnabled(false);


            }


        }
    });

    editText.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {

            if (motionEvent.getAction() == MotionEvent.ACTION_DOWN){

                editText.setEnabled(true);

                String userText = editText.getText().toString();

                editText.setText(userText.substring(0, userText.length() - 1));

                counterLabel.setTextColor(Color.parseColor("#FFFFFF"));

                counterLabel.setText(characterMax - editText.length() + " characters remaining");

            }

            return true;
        }
    });

【问题讨论】:

    标签: android android-edittext counter


    【解决方案1】:

    也许您可以在 XML 中定义 Edittext 的最大长度。 使用 maxLength 选项。

         <EditText
            android:id="@+id/EditText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:maxLength="196" >
        </EditText>
    

    【讨论】:

      【解决方案2】:

      哇。我最初从在 xml 中设置 maxLength 开始,但是在 addTextChangedListener 中的 IF 语句中使用 characterMax 代替 editText,这不起作用。因此,我从 xml 中删除了 maxLength 属性,并开始尝试其他事情。终于意识到我在我的 IF 语句中犯了一个错误,并使该逻辑正常工作,但继续尝试以编程方式禁用/启用 EditText,但没有运气。事实证明,它真的就像设置 maxLength 属性一样简单。对 Xcode 中需要的额外步骤感到困惑,并认为这在 Android 中同样乏味;男孩是我错了!这是我最终得到的解决方案,以及上面 user3347882 添加的 xml 建议。

      ...
      @Override
              public void afterTextChanged(Editable editable) {
      
                  counterLabel.setText(characterMax - editable.toString().length() + " characters remaining");
      
      
                  if (editText.length() == characterMax){
      
                      counterLabel.setTextColor(Color.parseColor("#FF0000"));
      
      
                  }
                  else{
      
                      counterLabel.setTextColor(Color.parseColor("#FFFFFF"));
      
                  }
      
      
      
              }
          });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多