登陆界面的数据保存回显的操作 (一)

2014-11-24 10:55:34 · 作者: · 浏览: 9

[java]


[java]
package com.example.day02_file;

import java.util.Map;

import com.example.lession02_file.service.FileService;

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;

public class LoginActivity extends Activity {

public static FileService fileService = null;
// 声明获取得用户与密码的组件
public EditText edit_name, edit_pass;
// 声明登陆按钮对象
public Button btn_login;
// 声明CheckBox组件对象
public CheckBox box_remember;

@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_remember = (CheckBox) findViewById(R.id.file_Chickbox);

btn_login.setOnClickListener(new MyOnClickListener());

// 回显数据
Map map = fileService.readFile("private.txt");
if (map != null) {
edit_name.setText(map.get("edit_name"));
edit_pass.setText(map.get("edit_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();

if (id == btn_login.getId()) {
String name = edit_name.getText().toString();
String pass = edit_pass.getText().toString();
if (TextUtils.isEmpty(name) || TextUtils.isEmpty(pass)) {
Toast.makeText(LoginActivity.this, "用户名或密码不能为空",
Toast.LENGTH_LONG).show();
return;

} else {
// 如果记住密码勾选上了
if (box_remember.isChecked()) {
// 进行保存
// 调用业务对象的业务方法
LoginActivity.this.fileService.saveToRom(name, pass,
"private.txt");
Toast.makeText(LoginActivity.this, "用户名和密码需要保存",
Toast.LENGTH_LONG).show();

} else {

// 不保存
Toast.makeText(LoginActivity.this, "用户名和密码不需要保存",
Toast.LENGTH_LONG).show();
}
}

}

}

}

}

package com.example.day02_file;

import java.util.Map;

import com.example.lession02_file.service.FileService;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
imp