Activity学习日记(六)
- 实现内部文件存储
package com.hq.myapp;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
public class LoginActivity extends Activity {
private final String TAG = "MyApp";
Button btn_login;
Button btn_exit;
EditText et_username;
EditText et_passwd;
CheckBox cb_auto_login;
CheckBox cb_mark_pwd;
SharedPreferences myappSetting;
SharedPreferences.Editor myEditor;
void myToast()
{
//来个吐司--用于调试/提示
//自定义一个吐司
Toast t = Toast.makeText(LoginActivity.this, "自定义吐司", Toast.LENGTH_SHORT);
t.setGravity(Gravity.CENTER, 0, 0);
LinearLayout layout = (LinearLayout) t.getView(); //得到一个view
ImageView image = new ImageView(LoginActivity.this);
image.setImageResource(R.drawable.head3);
layout.addView(image);
t.show();
}
//必须有一个监听器
OnClickListener btnClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d(TAG, "点击了,666");
// 1,获取两个输入框的内容
String username = et_username.getText().toString().trim();
String passwd = et_passwd.getText().toString().trim();
// 2, 比对内容
if(!username.isEmpty() && !passwd.isEmpty()){
if(username.equals("root") && passwd.equals("123456")){
myToast();
// 3, 比对密码成功就进入另外一个界面xxx.class是所有类中一个程序,记录对应类的信息
/*
// 第一种启动方式--显示启动
Intent intent = new Intent(LoginActivity.this, RegiterActivity.class);
startActivity(intent);
*/
//如果记住密码被选中
if(cb_mark_pwd.isChecked())
{
//将密码和用户名保存起来
myEditor.putString("usernameKey", username);
myEditor.putString("passwdKey", passwd);
myEditor.putBoolean("isMarkKey", cb_mark_pwd.isChecked());
myEditor.putBoolean("isAutoKey", cb_auto_login.isChecked());
myEditor.commit();
}else{
myEditor.putString("usernameKey", "");
myEditor.putString("passwdKey", "");
myEditor.putBoolean("isMarkKey", cb_mark_pwd.isChecked());
myEditor.putBoolean("isAutoKey", cb_auto_login.isChecked());
myEditor.commit();
}
//第二种启动方式---隐式启动
Intent intent = new Intent(); //已经加了默认的分类
intent.setAction("com.hq.test.action");
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.addCategory("com.hq.my.cateroy");
intent.setData(Uri.parse("MyData://my.host/10010"));
startActivity(intent);
}else{
Toast.makeText(LoginActivity.this, "用户或者密码错误", 1000).show();
}
}else{
Toast.makeText(LoginActivity.this, "用户或者密码不能为空", 1000).show();
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//其实该代码已经对xml中所有的标签进行分析,已经new所有控件
setContentView(R.layout.activity_login);
//初始化sharepreferce
myappSetting = getSharedPreferences("myapp_setting", MODE_PRIVATE);
myEditor = myappSetting.edit();
initUI();
//需要将xml文件中设置读出,并设置对应ui
//先判断是否有数据
if(!myappSetting.getAll().isEmpty()){
String name = myappSetting.getString("usernameKey", "george");
et_username.setText(name);
String passwd = myappSetting.getString("passwdKey", "1");
et_passwd.setText(passwd);
cb_mark_pwd.setChecked(myappSetting.getBoolean("isMarkKey", false));
cb_auto_login.setChecked(myappSetting.getBoolean("isAutoKey", false));
}
}
private void initUI() {
// TODO Auto-generated method stub
cb_auto_login = (CheckBox) findViewById(R.id.cb_auto_login);
cb_mark_pwd = (CheckBox) findViewById(R.id.cb_mark_pwd);
et_passwd = (EditText) findViewById(R.id.et_passwd);
et_username = (EditText) findViewById(R.id.et_username);
//获取到ui控件
btn_login = (Button) findViewById(R.id.btn_login);
//绑定事件源和监听器
btn_login.setOnClickListener(btnClickListener);
btn_exit = (Button) findViewById(R.id.btn_exit);
btn_exit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 先要创造建造工--创建dialog对象的对象
AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
//设置icon图标
builder.setIcon(R.drawable.button_share);
//设置标题栏
builder.setTitle("退出对话框");
// 设置消息体
builder.setMessage("你真的那么忍心就退出吗");
// 设置两个按钮
builder.setNegativeButton("再玩会", null);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
System.exit(1);
}
});
//创造对话框
builder.create().show();
}
});
}
}
package com.hq.myapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class RegiterActivity extends Activity{
private final String TAG = "MyApp";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
Log.d(TAG, "----------onCreate()------------");
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.d(TAG, "----------onStart()------------");
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.d(TAG, "----------onResume()------------");
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
Log.d(TAG, "----------onRestart()------------");
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.d(TAG, "----------onPause()------------");
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.d(TAG, "----------onStop()------------");
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.d(TAG, "----------onDestroy()------------");
}
}
package com.hq.myapp;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class ThirdActivity extends Activity implements OnClickListener{
Button btn_test;
Button btn_test2;
EditText et_show_text;
File defaultFilePath;
File notebookDir;
private static int count=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.acitivy_third);
initUI();
Intent intent = getIntent();
String dataString = intent.getData().toString();
String scheme = intent.getData().getScheme();
String host = intent.getData().getHost();
String path = intent.getData().getPath();
et_show_text.setText("dataString = " + dataString + "\n" +
"scheme = " + scheme + "\n" +
"host = " + host + "\n" +
"path = " + path
);
//先产生一个文件夹
defaultFilePath = getFilesDir();
notebookDir = new File(defaultFilePath, "notebook");
if(!notebookDir.exists()){
notebookDir.mkdir();
}
}
private void initUI() {
// TODO Auto-generated method stub
btn_test = (Button) findViewById(R.id.btn_test);
btn_test.setOnClickListener(this);
btn_test2 = (Button) findViewById(R.id.btn_test2);
btn_test2.setOnClickListener(this);
et_show_text = (EditText) findViewById(R.id.et_show_text);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 区分不同的按钮
switch (v.getId()) {
case R.id.btn_test:
// 获取到输入框的数据
String content = et_show_text.getText().toString().trim();
//创建文件并写入数据到文件
if(!content.isEmpty()){
File noteFile = new File(notebookDir, "note"+(count++)+".txt");
if(!noteFile.exists()){
//创建新文件
try {
noteFile.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
FileOutputStream out;
try {
out = new FileOutputStream(noteFile);
out.write(content.getBytes());
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}else{
Toast.makeText(ThirdActivity.this, "内容不能为空", 1000).show();
}
//
break;
case R.id.btn_test2:
Toast.makeText(ThirdActivity.this, "测试2按钮点击666", 1000).show();
break;
default:
break;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/et_show_text"
android:hint="输入文字"
android:gravity="start"
android:layout_width="match_parent"
android:layout_height="200dp"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/btn_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记笔记" />
<Button
android:id="@+id/btn_test2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="测试2" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/tv_user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:layout_marginTop="34dp"
android:text="用户名"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/et_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/tv_user_name"
android:layout_alignBottom="@+id/tv_user_name"
android:layout_toRightOf="@id/tv_user_name"
android:layout_marginLeft="30dp"
android:ems="10"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/tv_passwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tv_user_name"
android:layout_below="@+id/tv_user_name"
android:layout_marginTop="59dp"
android:text="密 码"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/et_passwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/tv_passwd"
android:layout_alignLeft="@+id/et_username"
android:ems="10"
android:inputType="textPassword" />
<CheckBox
android:id="@+id/cb_mark_pwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tv_passwd"
android:layout_centerVertical="true"
android:text="记住密码" />
<CheckBox
android:id="@+id/cb_auto_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/cb_mark_pwd"
android:layout_marginLeft="45dp"
android:layout_toRightOf="@+id/cb_mark_pwd"
android:text="自动登录" />
<Button
android:id="@+id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/cb_mark_pwd"
android:layout_below="@+id/cb_mark_pwd"
android:layout_marginTop="67dp"
android:text="登录" />
<Button
android:id="@+id/btn_exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/btn_login"
android:layout_alignRight="@+id/et_username"
android:layout_marginRight="16dp"
android:text="退出" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/bg1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="欢迎注册百合网"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/et_input_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView2"
android:layout_alignLeft="@+id/textView1"
android:ems="10" />
<Spinner
android:id="@+id/spinner_work"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView3"
android:layout_alignLeft="@+id/et_input_name"
android:entries="@array/sex_array" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/textView3"
android:layout_marginTop="33dp"
android:text="性别"
android:textAppearance="?android:attr/textAppearanceLarge" />
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView4"
android:layout_alignLeft="@+id/spinner_work"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/rb_man"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男" />
<RadioButton
android:id="@+id/rb_women"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女" />
<RadioButton
android:id="@+id/rb_unkown"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="人妖" />
</RadioGroup>
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView4"
android:layout_below="@+id/textView4"
android:layout_marginTop="38dp"
android:text="爱好"
android:textAppearance="?android:attr/textAppearanceLarge" />
<CheckBox
android:id="@+id/cb_movie"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView5"
android:layout_alignBottom="@+id/textView5"
android:layout_alignLeft="@+id/radioGroup1"
android:text="看电影" />
<CheckBox
android:id="@+id/cb_game"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/cb_movie"
android:layout_alignBottom="@+id/cb_movie"
android:layout_marginLeft="20dp"
android:layout_toRightOf="@+id/cb_movie"
android:text="打游戏" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="23dp"
android:scaleType="fitCenter"
android:src="@drawable/head3" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/imageView1"
android:layout_marginLeft="14dp"
android:layout_marginTop="23dp"
android:text="姓名:"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView2"
android:layout_below="@+id/textView2"
android:layout_marginTop="24dp"
android:text="职业"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/btn_submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/cb_movie"
android:layout_marginTop="97dp"
android:text="提交" />
<Button
android:id="@+id/btn_register_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/btn_submit"
android:layout_alignRight="@+id/et_input_name"
android:layout_marginBottom="7dp"
android:text="取消" />
</RelativeLayout>