@[toc]{今日作业}
package com.example.day1016_3;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.day1016_3.sql.MySqlHeloUser;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainActivity extends AppCompatActivity {
private EditText loginTel;
private EditText loginPwd;
private Button loginButtonLogin;
private Button loginButtonForgetPwd;
private Button loginButtonRegisterNow;
private SQLiteDatabase db;
private String usertel;
private String userpwd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
loginTel = (EditText) findViewById(R.id.login_tel);
loginPwd = (EditText) findViewById(R.id.login_pwd);
loginButtonLogin = (Button) findViewById(R.id.login_button_login);
loginButtonForgetPwd = (Button) findViewById(R.id.login_button_forgetPwd);
loginButtonRegisterNow = (Button) findViewById(R.id.login_button_registerNow);
MySqlHeloUser mySqlHeloUser = new MySqlHeloUser(this,"user.db",null,1);
db = mySqlHeloUser.getReadableDatabase();
loginButtonLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String tel = loginTel.getText().toString();
String pwd = loginPwd.getText().toString();
Cursor query = db.query("user", null, null, null, null, null, null);
if (query!=null){
while (query.moveToNext()){
usertel = query.getString(query.getColumnIndex("tel"));
userpwd = query.getString(query.getColumnIndex("pwd"));
}
}
if (tel.equals(usertel)&&pwd.equals(userpwd)) {
Toast.makeText(MainActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this, "登陆失败", Toast.LENGTH_SHORT).show();
}
}
});
loginButtonRegisterNow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
}
});
}
}
package com.example.day1016_3;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.day1016_3.sql.MySqlHelp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main2Activity extends AppCompatActivity {
private Button registerButtonReturn;
private EditText registerTel;
private EditText registerYzm;
private Button registerButtonSendYzm;
private EditText registerPwd;
private Button registerButtonRegisterNow;
private Notification.Builder builder;
private NotificationManager manager;
private int yzm;
private SQLiteDatabase db;
private SQLiteDatabase dbuser;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
initView();
}
private void initView() {
registerButtonReturn = (Button) findViewById(R.id.register_button_return);
registerTel = (EditText) findViewById(R.id.register_tel);
registerYzm = (EditText) findViewById(R.id.register_yzm);
registerButtonSendYzm = (Button) findViewById(R.id.register_button_sendYzm);
registerPwd = (EditText) findViewById(R.id.register_Pwd);
registerButtonRegisterNow = (Button) findViewById(R.id.register_button_registerNow);
//获取权限
//获取数据库
MySqlHelp mySqlHelp = new MySqlHelp(this,"phone.db",null,1);
MySqlHelp mySqlHelp1 = new MySqlHelp(this,"user.db",null,1);
db = mySqlHelp.getReadableDatabase();
dbuser = mySqlHelp1.getReadableDatabase();
//通知
builder=new Notification.Builder(this);
manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
registerButtonSendYzm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String tel = registerTel.getText().toString();
if (tel==null||tel.equals("")){
Toast.makeText(Main2Activity.this, "请先输入您的手机号码", Toast.LENGTH_SHORT).show();
return;
}
Cursor query = db.query("phone",null, null, null, null,null,null);
if (query!=null){
query.moveToFirst();
yzm = query.getInt(query.getColumnIndex("yzm"));
}
builder.setSmallIcon(R.drawable.ic_launcher_background);
builder.setContentTitle("您有一条新的消息:");
builder.setContentText("您的验证码是:"+yzm);
builder.setAutoCancel(true);
manager.notify(1,builder.build());
Log.i("TAG", "onReceive: yzm"+yzm);
Intent intent=new Intent();
intent.setAction("com.mogu.day1016");
Bundle bundle=new Bundle();
bundle.putInt("yzm",yzm);
intent.putExtras(bundle);
sendBroadcast(intent);
query.close();
}
});
registerButtonRegisterNow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String pwd = registerPwd.getText().toString();
if (pwd!=null) {
Pattern compile = Pattern.compile("[\\w]{6,16}");
Matcher matcher = compile.matcher(registerPwd.getText().toString());
boolean matches = matcher.matches();
if (!matches){
Toast.makeText(Main2Activity.this, "填入密码要求为6-16位数字字母组合", Toast.LENGTH_SHORT).show();
return;
}
}
String yzmString = registerYzm.getText().toString();
int anInt = Integer.parseInt(yzmString);
if (anInt!=yzm){
Toast.makeText(Main2Activity.this, "验证码错误", Toast.LENGTH_SHORT).show();
return;
}
String tel = registerTel.getText().toString();
ContentValues values = new ContentValues();
values.put("tel",tel);
values.put("pwd",pwd);
dbuser.insert("user",null,values);
Intent intent=new Intent(Main2Activity.this,MainActivity.class);
startActivity(intent);
}
});
}
}
package com.example.day1016_3.sql;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.annotation.Nullable;
public class MySqlHelp extends SQLiteOpenHelper {
public MySqlHelp(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table phone(yzm integer)");
db.beginTransaction();
for (int i = 0; i < 10; i++) {
String sql="insert into phone values(?)";
db.execSQL(sql,new Object[]{123+i});
}
db.setTransactionSuccessful();
db.endTransaction();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
package com.example.day1016_3.sql;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.annotation.Nullable;
public class MySqlHeloUser extends SQLiteOpenHelper {
public MySqlHeloUser(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table user(tel varchar(20),pwd varchar(20))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
package com.example.day1016_3;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals("com.mogu.day1016")){
Bundle bundle = intent.getExtras();
int yzm = bundle.getInt("yzm");
Toast.makeText(context, "得到的验证码:"+yzm, Toast.LENGTH_SHORT).show();
Log.i("TAG", "onReceive: yzm"+yzm);
}
}
}
<?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"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@mipmap/lu"></ImageView>
<EditText
android:id="@+id/login_tel"
android:hint="手机号码"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"></EditText>
<EditText
android:id="@+id/login_pwd"
android:hint="密码"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"></EditText>
<Button
android:id="@+id/login_button_login"
android:textColor="#fff"
android:textSize="20dp"
android:text="登陆"
android:layout_margin="30dp"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@drawable/item_ciyle"></Button>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/login_button_forgetPwd"
android:layout_gravity="center"
style="?android:attr/borderlessButtonStyle"
android:layout_width="0dp"
android:layout_weight="3"
android:text="忘记密码"
android:gravity="right"
android:layout_height="wrap_content"></Button>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:text="|"
android:gravity="center"
android:layout_gravity="center"
android:layout_height="match_parent"></TextView>
<Button
android:id="@+id/login_button_registerNow"
android:gravity="left"
style="?android:attr/borderlessButtonStyle"
android:text="现在注册"
android:layout_width="0dp"
android:layout_weight="3"
android:layout_height="wrap_content"></Button>
</LinearLayout>
</LinearLayout>
</LinearLayout>
``
### register布局
```java
<?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:orientation="vertical"
android:layout_height="match_parent"
tools:context=".Main2Activity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="#FE7AA2">
<Button
android:id="@+id/register_button_return"
android:textColor="#fff"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="<"
android:textSize="20dp"
style="?android:attr/borderlessButtonStyle"></Button>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="注册"
android:textColor="#fff"
android:gravity="center"
android:layout_centerInParent="true"
android:textSize="20sp"></TextView>
</RelativeLayout>
<EditText
android:id="@+id/register_tel"
android:hint="手机号码"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"></EditText>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/register_yzm"
android:hint="验证码"
android:inputType="textPassword"
android:layout_width="230dp"
android:layout_height="60dp"
android:layout_margin="20dp"></EditText>
<Button
android:id="@+id/register_button_sendYzm"
android:layout_width="wrap_content"
android:layout_height="90dp"
android:text="发送验证码"
android:textSize="20dp"
android:textColor="#FE7AA2"
android:layout_marginRight="20dp"
android:layout_alignParentRight="true"
style="?android:attr/borderlessButtonStyle"
></Button>
</RelativeLayout>
<EditText
android:id="@+id/register_Pwd"
android:hint="登陆密码(6-16位数字字母综合密码)"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"></EditText>
<Button
android:id="@+id/register_button_registerNow"
android:textColor="#fff"
android:textSize="20dp"
android:text="立即注册"
android:layout_margin="30dp"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@drawable/item_ciyle"></Button>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_gravity="center"
android:layout_width="0dp"
android:layout_weight="3"
android:text="注册代表您同意"
android:gravity="right"
android:layout_height="wrap_content"></TextView>
<TextView
android:gravity="left"
android:textColor="#FE7AA2"
android:text="《用户协议》"
android:layout_width="0dp"
android:layout_weight="3"
android:layout_height="wrap_content"></TextView>
</LinearLayout>
</LinearLayout>
</LinearLayout>