Android_登录界面功能和实现详解

功能介绍:

两个编辑框

一个输入账号 ,一个输入密码

一个按钮

点击按钮登录

判断账号和密码不为空

设置账号必须以字母开头

验证密码最小为8位,最大16位

设置可显示隐藏密码


示例代码:

public class MainActivity extends AppCompatActivity {
    private EditText et_name;
    private EditText et_pass;
    SharedPreferences share;
    private CheckBox cb;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_name = (EditText)findViewById(R.id.et_name);
        et_pass = (EditText)findViewById(R.id.et_pass);
        cb = (CheckBox)findViewById(R.id.cb);
        cb.setOnCheckedChangeListener(new CheckClick());
        share = getSharedPreferences("login",MODE_PRIVATE);
        String name = share.getString("name", "");
        String pass = share.getString("pass", "");
        if (!name.isEmpty() && !pass.isEmpty()){
            et_name.setText(name);
            et_pass.setText(pass);
        }
    }
    public void login(View view){
        String name = et_name.getText().toString();
        String pass = et_pass.getText().toString();
        Log.i("MainActivity",name.isEmpty()+"");
        if (name.isEmpty()||pass.isEmpty()){
            Toast.makeText(this, "账号和密码都不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        String first = name.substring(0,1);
        if(!first.matches("[A-Za-z]")){
            Toast.makeText(this, "用户名必须以字母开头", Toast.LENGTH_SHORT).show();
            return;
        }
        Log.i("MainActivity",""+first);
        Log.i("MainActivity",""+first.matches("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
        if (pass.length()>=8 && pass.length()<=16){
            SharedPreferences.Editor edit = share.edit();
            edit.putString("name",name);
            edit.putString("pass",pass);
            edit.commit();
            Toast.makeText(this, "登录成功,", Toast.LENGTH_SHORT).show();
//            Intent intent = new Intent(MainActivity.this,Main2Activity.class);
//            startActivity(intent);
        }else {
            Toast.makeText(this, "密码必须为8-16位", Toast.LENGTH_SHORT).show();
        }
    }
    class CheckClick implements CompoundButton.OnCheckedChangeListener{
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            Log.i("MainActivity",""+isChecked);
            if (isChecked){
                et_pass.setInputType(InputType.TYPE_NUMBER_VARIATION_PASSWORD);
                cb.setText("隐藏密码");
            }else {
                et_pass.setInputType(InputType.TYPE_TEXT_VARIATION_FILTER);
                cb.setText("显示密码");
            }
        }
    }
}

XML:




    
    
    
    

本章完。

你可能感兴趣的:(Android_登录界面功能和实现详解)