Herjey
HotCoffeeCup
2017-11-10 13:20

安卓的焦点事件和textwatch之间的问题

  • android

动态判断输入框的状况,我在textwatch连获取edittext的值,然后在焦点事件立运用这几个值
每次开始输入的时候就报错退出了,求大神回答
小白君

package com.example.homewor2;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class Register extends AppCompatActivity implements View.OnClickListener{

EditText account,password,surepassword,mail,phone;
String s_account,s_password,s_surepassword,s_mail,s_phone;
View register;
TextView warming_tip;
private final int maxcharsequenceNum=18;
EditTextListen et ;

public void initdate(){

    et = new EditTextListen();

    account = (EditText)findViewById(R.id.register_account_edit);
    account.addTextChangedListener(new EditTextListen());
    account.setOnFocusChangeListener(new onfockchange());

    password = (EditText)findViewById(R.id.register_password_edit);
    password.addTextChangedListener(new EditTextListen());
    password.setOnFocusChangeListener(new onfockchange());

    surepassword = (EditText)findViewById(R.id.register_sure_password_edit);
    surepassword.addTextChangedListener(new EditTextListen());
    surepassword.setOnFocusChangeListener(new onfockchange());

    mail = (EditText)findViewById(R.id.register_mail_edit);
    phone = (EditText)findViewById(R.id.register_phone_edit);

    warming_tip = (TextView)findViewById(R.id.worming_tip);

    register = (View) findViewById(R.id.register_layout);
    register.setOnClickListener(this);


}


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

    initdate();

    ActionBar ab = getSupportActionBar();
    if (ab!=null){
        ab.hide();
    }
}

//按键监听
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.register_layout:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
break;
}
}

//处理editText的监听
class EditTextListen implements TextWatcher{
    CharSequence cs;
    private int start;
    private int now ;
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        this.cs = s;

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


        warming_tip.setText(account.length() + "/" + maxcharsequenceNum);

            if (account.length() >= maxcharsequenceNum) {
                warming_tip.setTextColor(Color.rgb(249, 3, 3));
            } else {
                warming_tip.setTextColor(Color.rgb(0, 0, 0));
            }
    }
    @Override
    public void afterTextChanged(Editable s) {


        start =account.getSelectionStart();
        now = account.getSelectionEnd();

        if (cs.length()>maxcharsequenceNum){
            s.delete(start-1,now);

// account.setText(s);
// account.setSelection(s.length());
}

        s_account = account.getText().toString();
        s_password=password.getText().toString();
        s_surepassword=surepassword.getText().toString();

    }
}
//焦点事件
class onfockchange implements View.OnFocusChangeListener{

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus){

        }else{
            if (s_account.length()<5){
                account.setError("账号必须在5-18个字符之间");
            }
            if (s_password.length()<8){
                password.setError("密码不可以少于8位数");
            }
            if (!s_surepassword.equals(s_password)){
                surepassword.setError("您两次输入的密码不一致");
            }
        }
    }
}

}

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答