最近在准备开题报告,已经很久没再写博客了,明天要开题答辩了,十分紧张,写个博客,放松一下,祝自己明天顺利通过。哈哈!!!!!
前一阵子,学习了一下java安卓开发,做了一个小东西,就是自己用玩的。本来还要写一些关于智能算法和React的程序,但是时间恐怕不太够了,算了,等下次有时间再写吧,反正技多不压身。
程序的目录
ActivityCollector.java
package com.example.myhomework;
import android.app.Activity;
import java.util.ArrayList;
import java.util.List;
public class ActivityCollector {
public static List<Activity> activities = new ArrayList<>();
public static void addActivity(Activity activity) {
activities.add(activity);
}
public static void removeActivity(Activity activity) {
activities.remove(activity);
}
public static void finishAll() {
for (Activity activity : activities){
if (!activity.isFinishing()){
activity.finish();
}
}
}
}
BaseActivity.java
package com.example.myhomework;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
public class BaseActivity extends AppCompatActivity {
private ForceOfflineReceiver receiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v("BaseActivity","广播接收器已创建");
ActivityCollector.addActivity(this);
}
@Override
protected void onResume() {
super.onResume();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.example.myhomework.FORCE_OFFLINE");
receiver = new ForceOfflineReceiver();
registerReceiver(receiver, intentFilter);
Log.v("BaseActivity","广播接收器已接受");
}
@Override
protected void onPause() {
super.onPause();
if (receiver != null){
unregisterReceiver(receiver);
receiver = null;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
ActivityCollector.removeActivity(this);
}
private class ForceOfflineReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Waring");
builder.setMessage("You are forced to be offline. Please try to login again.");
builder.setCancelable(false);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCollector.finishAll();//销毁所有活动
Intent intent = new Intent(context, LoginActivity.class);
context.startActivity(intent);//重新启动LoginActivity
}
});
builder.show();
}
}
}
LoginActivity.java
package com.example.myhomework;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class LoginActivity extends BaseActivity {
private SharedPreferences pref;
private SharedPreferences.Editor editor;
private EditText accountEdit;
private EditText passwordEdit;
private Button login;
private Button register;
private Button quertData;
private CheckBox rememberPass;
private MyDatabaseHelper dbHelper;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
// 隐藏自带标题
ActionBar actionBar = getSupportActionBar();
if (actionBar != null){
actionBar.hide();
}
pref = PreferenceManager.getDefaultSharedPreferences(this);
accountEdit = (EditText) findViewById(R.id.account);
passwordEdit = (EditText) findViewById(R.id.password);
rememberPass = (CheckBox) findViewById(R.id.remember_pass);
login = (Button) findViewById(R.id.login);
register = (Button) findViewById(R.id.register);
quertData = (Button) findViewById(R.id.query_data);
dbHelper = new MyDatabaseHelper(this, "BookStore.db", null, 2);
Button createDatabase = (Button) findViewById(R.id.create_database);
createDatabase.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dbHelper.getWritableDatabase();
}
});
boolean isRemember = pref.getBoolean("remember_password", false);
if (isRemember){
// 将账号和密码都设置到文本框中
String account = pref.getString("account", "");
String password = pref.getString("password", "");
accountEdit.setText(account);
passwordEdit.setText(password);
rememberPass.setChecked(true);
}
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String account = accountEdit.getText().toString();
String password = passwordEdit.getText().toString();
// 如果账号是admin密码是123456,就认为登陆成功
// account.equals("admin") && password.equals("123456")
if(checkExist(account,password)){
editor = pref.edit();
if (rememberPass.isChecked()) { //检查复选框是否被选中
editor.putBoolean("remember_password", true);
editor.putString("account",account);
editor.putString("password", password);
}else{
editor.clear();
}
editor.apply();
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
}else{
Toast.makeText(LoginActivity.this, "account or wassspord is invalid", Toast.LENGTH_SHORT).show();
}
}
});
register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
startActivity(intent);
}
});
quertData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
Log.d("LoginActivity", "开始遍历");
// 查询表中所有数据
Cursor cursor = db.query("Book", null, null, null,null,null,null);
if (cursor.moveToFirst()){
do{
//遍历Cursor对象,取出数据并打印
String account = cursor.getString(cursor.getColumnIndex("account"));
String password = cursor.getString(cursor.getColumnIndex("password"));
Log.d("LoginActivity", "账号"+account+"\t"+"密码"+ password);
} while (cursor.moveToNext());
}
cursor.close();
}
});
}
public Boolean checkExist(String a,String p){
dbHelper = new MyDatabaseHelper(this, "BookStore.db", null, 2);
SQLiteDatabase db = dbHelper.getWritableDatabase();
Log.d("LoginActivity", "开始遍历");
// 查询表中所有数据
Cursor cursor = db.query("Book", null, null, null,null,null,null);
if (cursor.moveToFirst()){
do{
//遍历Cursor对象,取出数据并打印
String account = cursor.getString(cursor.getColumnIndex("account"));
String password = cursor.getString(cursor.getColumnIndex("password"));
if(a.equals(account) && p.equals(password)){
return true;
}
Log.d("LoginActivity", "账号"+account+"\t"+"密码"+ password);
} while (cursor.moveToNext());
}
cursor.close();
return false;
}
}
MainActivity.java
package com.example.myhomework;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button forceOffline = (Button) findViewById(R.id.force_offline);
forceOffline.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("com.example.myhomework.FORCE_OFFLINE");
sendBroadcast(intent);
Log.v("MainActivity","已发送广播信息");
}
});
}
}
MyDatabaseHelper.java
package com.example.myhomework;
import android.content.Context;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;
public class MyDatabaseHelper extends SQLiteOpenHelper {
public static final String CREATE_TABLE = "create table Book ("
+"id integer primary key autoincrement, "
+"account text,"
+"password text)";
private Context mContext;
public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
mContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
Toast.makeText(mContext, "Create successed", Toast.LENGTH_SHORT).show();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists Book");
onCreate(db);
}
}
RegisterActivity.java
package com.example.myhomework;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class RegisterActivity extends BaseActivity {
private MyDatabaseHelper dbHelper;
private Button submit;
private Button clear;
private TextView account;
private TextView password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
dbHelper = new MyDatabaseHelper(this, "BookStore.db", null, 2);
submit = (Button) findViewById(R.id.submit);
clear = (Button) findViewById(R.id.clear);
account = (TextView) findViewById(R.id.re_account);
password = (TextView) findViewById(R.id.re_password);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
// 开始添加第一条数据
String a = account.getText().toString();
String b = password.getText().toString();
Log.d("RegisterActivity",a+"\t"+b);
values.put("account",a);
values.put("password",password.getText().toString());
db.insert("Book", null, values);
// Log.d("RegisterActivity",account.getText().toString()+"\t"+password.getText().toString());
values.clear();
Toast.makeText(RegisterActivity.this, "添加完成", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);
startActivity(intent);
}
});
clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
account.setText("");
password.setText("");
}
});
}
}
Titlelayout.java
package com.example.myhomework;
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
public class Titlelayout extends LinearLayout {
public Titlelayout(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.title, this);
Button titleback = (Button) findViewById(R.id.title_back);
Button titleEdit = (Button) findViewById(R.id.title_edit);
titleback.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
((Activity) getContext() ).finish();
}
});
titleEdit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(), "人间自有真情在,多给两分行不行?", Toast.LENGTH_LONG).show();
}
});
}
}
布局的程序
activity_login.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
>
<com.example.myhomework.Titlelayout
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="92dp"
android:layout_marginTop="30dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="100dp"
android:text="学生登录界面"
android:textSize="50sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="40dp"
android:orientation="horizontal">
<TextView
android:layout_width="90dp"
android:layout_height="wrap_content"
android:text="Account"
android:textSize="18sp" />
<EditText
android:id="@+id/account"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="60dp">
<TextView
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="18sp"
android:text="Password"
/>
<EditText
android:id="@+id/password"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:inputType="textPassword"
/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_marginTop="20dp"
android:layout_marginLeft="50dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/remember_pass"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="Remember password"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/login"
android:layout_width="167dp"
android:layout_height="60dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:layout_marginRight="10dp"
android:text="Login" />
<Button
android:id="@+id/register"
android:layout_width="167dp"
android:layout_height="60dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:layout_marginRight="10dp"
android:text="Register" />
</LinearLayout>
<Button
android:id="@+id/create_database"
android:layout_marginTop="40dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Create Table" />
<Button
android:id="@+id/query_data"
android:layout_marginTop="40dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查询表中数据并打印" />
</LinearLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/force_offline"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send force offline broadcast"
/>
</LinearLayout>
activity_register.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="92dp"
android:layout_marginTop="30dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="50dp"
android:text="学生注册界面"
android:textSize="50sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="40dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:orientation="horizontal">
<TextView
android:layout_width="90dp"
android:layout_height="wrap_content"
android:text="姓名"
android:textSize="18sp" />
<EditText
android:id="@+id/re_account"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_height="60dp">
<TextView
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="18sp"
android:text="学号"
/>
<EditText
android:id="@+id/re_password"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:inputType="textPassword"
/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_height="60dp">
<TextView
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="18sp"
android:text="手机号"
/>
<EditText
android:id="@+id/re_phone"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"
/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_height="60dp">
<TextView
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="18sp"
android:text="性别"
/>
<EditText
android:id="@+id/re_xingbie"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/submit"
android:layout_width="167dp"
android:layout_height="60dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:layout_marginRight="10dp"
android:text="Submit" />
<Button
android:id="@+id/clear"
android:layout_width="167dp"
android:layout_height="60dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:layout_marginRight="10dp"
android:text="Clear" />
</LinearLayout>
</LinearLayout>
titile.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity">
<Button
android:id="@+id/title_back"
android:layout_width="50dp"
android:layout_height="65dp"
android:layout_gravity="center"
android:layout_margin="5dp"
android:background="@drawable/back"
android:textColor="#fff" />
<TextView
android:id="@+id/title_text"
android:layout_width="0dp"
android:layout_height="42dp"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="请点击右边的按钮-->"
android:textColor="#000"
android:textSize="24sp" />
<Button
android:id="@+id/title_edit"
android:layout_width="53dp"
android:layout_height="61dp"
android:layout_gravity="center"
android:layout_margin="5dp"
android:background="@drawable/close"
android:textColor="#fff" />
</LinearLayout>
结果