Android开发(6)--完成登陆界面的数据保存回显的操作
LoginActivity.java
[java]
package com.example.login;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import com.example.login.service.FileService;
public class LoginActivityextends Activity {
public EditText edit_name,edit_pass;
public Button btn_login;
public CheckBox box_remeber;
public FileService fileService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
fileService=new FileService(this);
edit_name=(EditText) findViewById(R.id.edit_name);
edit_pass=(EditText) findViewById(R.id.edit_pass);
btn_login=(Button) findViewById(R.id.btn_login);
box_remeber=(CheckBox) findViewById(R.id.cbx_remember);
btn_login.setOnClickListener(new MyOnClickListener());
Map<String, String> map=fileService.readFile("private.txt");
if(map!=null){
edit_name.setText(map.get("name"));
edit_pass.setText(map.get("pass"));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.login, menu);
return true;
}
class MyOnClickListenerimplements View.OnClickListener{
@Override
public void onClick(View v) {
int id=v.getId();
switch (id) {
case R.id.btn_login:
String name=edit_name.getText().toString();
String pass=edit_pass.getText().toString();
if(TextUtils.isEmpty(name)){
Toast.makeText(LoginActivity.this,"用户名不能为空", Toast.LENGTH_SHORT).show();
return;
}else if(TextUtils.isEmpty(pass)){
Toast.makeText(LoginActivity.this,"密码不能为空", Toast.LENGTH_SHORT).show();
return;
}else{
if(box_remeber.isChecked()){
LoginActivity.this.fileService.saveToRom(name, pass,"private.txt");
Toast.makeText(LoginActivity.this,"用户名和密码已保存", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(LoginActivity.this,"用户名和密码不需要保存", Toast.LENGTH_SHORT).show();
}
}
break;
default:
break;
}
/*if(id==btn_login.getId()){
String name=edit_name.getText().toString();
String pass=edit_pass.getText().toString();
if(TextUtils.isEmpty(name)){
Toast.makeText(LoginActivity.this, "用户名不能为空", Toast.LENGTH_SHORT).show();
return;
}else if(TextUtils.isEmpty(pass)){
Toast.makeText(LoginActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
return;
}else{
if(box_remeber.isChecked()){
LoginActivity.this.fileService.saveToRom(name, pass, "private.txt");
Toast.makeText(LoginActivity.this, "用户名和密码已保存", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(LoginActivity.this, "用户名和密码不需要保存", Toast.LENGTH_SHORT).show();
}
}
}*/
}
}
}
[java]
package com.example.login;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import com.example.login.service.FileService;
public class LoginActivity extends Activity {
public EditText edit_name,edit_pass;
public Button btn_login;
public CheckBox box_remeber;
public FileService fileService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
fileService=new FileService(this);
edit_name=(EditText) findViewById(R.id.edit_name);
edit_pass=(EditText) findViewById(R.id.edit_pass);
btn_login=(Button) findViewById(R.id.btn_login);
box_remeber=(CheckBox) findViewById(R.id.cbx_remember);
btn_login.setOnClickListener(new MyOnClickListener());
Map<String, String> map=fileService.readFile("private.txt");
if(map!=null){
edit_name.setText(map.get("name"));
edit_pass.setText(map.get("pass"));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.login, menu);
return true;
}
class MyOnClickListener implements View.OnClickListener{
@Override
public void onClick(View v) {
int id=v.getId();
switch (id) {
case R.id.btn_login:
String name=edit_name.getText().toString();
String pass=edit_pass.getText().toString();
if(TextUtils.isEmpty(name)){
Toast.makeText(LoginActivity.this, "用户名不能为空", Toast.LENGTH_SHORT).show();
return;
}else if(TextUtils.isEmpty(pass)){
补充:移动开发 , Android ,