EditText基本用法



    

    
    
    

    

    


java代码

public class EditText extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        final android.widget.EditText editName,editPWD;
        final Button btnRegister;
        final TextView tvResult;
        super.onCreate(savedInstanceState);
        //显示界面与布局文件相关联
        setContentView(R.layout.test2);
        //代码与xml中控件相关联
        editName= (android.widget.EditText) findViewById(R.id.editName);
        editPWD= (android.widget.EditText) findViewById(R.id.editPWD);
        btnRegister=(Button) findViewById(R.id.btnRegister);
        tvResult=(TextView) findViewById(R.id.tvResult);
        btnRegister.setEnabled(false);
        editName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                 if(s.length()!=0){
                     btnRegister.setEnabled(true);
                     }else{
                     btnRegister.setEnabled(false);
                 }
            }

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

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        //3.注册事件
        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //实现事件
                String result="";
                result="用户名"+editName.getText()+""+"密码"+editPWD.getText();
                tvResult.setText(result+"");

            }
        });


    }
}



你可能感兴趣的