2019独角兽企业重金招聘Python工程师标准>>>
实现一个第一次登录成功切选择保存后第二次打开默认用户名:protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);final EditText edit1=(EditText) findViewById(R.id.editText1);final EditText edit2=(EditText) findViewById(R.id.editText2);final CheckBox check=(CheckBox) findViewById(R.id.checkBox1);final SharedPreferences pre=getSharedPreferences("mypre",MODE_PRIVATE);final Editor editor=pre.edit();final Button bt=(Button) findViewById(R.id.button1);String name=pre.getString("username","");if(name==null){check.setChecked(false);}else{edit1.setText(name);check.setChecked(true);}bt.setOnClickListener(new OnClickListener(){ @Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubString username=edit1.getText().toString().trim(); //trim()方法为去掉前后空白String password=edit2.getText().toString().trim();if("admin".equals(username)&&"123456".equals(password)){ //如果用户名或密码正确切check框为选中状态,则将用户名信息储存起来if(check.isChecked()){ editor.putString("username",username);editor.commit();}else{editor.remove(username); //不需要储存则删除editor.commit();}}else{Toast.makeText(MainActivity.this,"用户名或密码错误",Toast.LENGTH_LONG).show();}}});