我已經構造了一個計數器標籤TextView,它可以減少EditText中留下的字符數。我允許EditText中的196個字符。當EditText字符數達到零時,我將計數器標籤的文本顏色更改爲紅色,並禁用EditText,以便用戶不能輸入更多字符。EditText字符倒數問題
當我嘗試刪除爲了啓用用戶輸入而輸入的最後一個字符時,出現了我的問題。我想要做的是刪除最後一個字符,以便禁用EditText的邏輯不再爲true,EditText變爲啓用,計數器標籤的文本顏色變回白色,計數被更新。但是,我寫的代碼不起作用,並且EditText保持禁用狀態。我正在使用addTextChangedListener來禁用,並且使用onTouchListener嘗試並重新啓用。
我已經設法使用TOUCHESBEGAN方法來捕獲用戶的觸摸,啓用UITextView,使用DELETEBACKWARD函數刪除最後一個字符,並更新計數,從而使這項工作適用於iOS。有人可以研究我的代碼,並指出我在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;
}
});