今天完成了app界面和注册
<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:text="学号"
android:textColor="@android:color/holo_blue_bright"
android:textSize="8pt" />
<EditText
android:id="@+id/id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:textSize="8pt"
android:inputType="number"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:text="用户名"
android:textColor="@android:color/holo_blue_bright"
android:textSize="8pt" />
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:textSize="8pt"
android:inputType="text"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:text="手机号码"
android:textColor="@android:color/holo_blue_bright"
android:textSize="8pt" />
<EditText
android:id="@+id/phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:textSize="8pt"
android:inputType="text"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:text="班级"
android:textColor="@android:color/holo_blue_bright"
android:textSize="8pt" />
<EditText
android:id="@+id/unit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:textSize="8pt"
android:inputType="text"
/>
<Button
android:id="@+id/add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="10pt"
android:text="注册"
/>
</LinearLayout>
<?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">
<Button
android:id="@+id/b1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="注册"
android:textColor="@android:color/holo_orange_dark"
android:textSize="20pt" />
<Button
android:id="@+id/b2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="打卡"
android:textColor="@android:color/holo_orange_dark"
android:textSize="20pt" />
<Button
android:id="@+id/b3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="设置事件"
android:textColor="@android:color/holo_orange_dark"
android:textSize="20pt" />
</LinearLayout>
<?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"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:text="每日关键字"
android:textColor="@android:color/holo_blue_bright"
android:textSize="8pt" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:textSize="8pt"
android:inputType="text"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:text="每日总结"
android:textColor="@android:color/holo_blue_bright"
android:textSize="8pt" />
<EditText
android:layout_width="match_parent"
android:layout_height="300dp"
android:textColor="@android:color/holo_purple"
android:layout_margin="15dp"
android:textSize="8pt"
android:background="@android:drawable/edit_text"
android:inputType="text"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="10pt"
android:text="增加"
/>
</LinearLayout>
package com.example.stusign;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b1=findViewById(R.id.b1);
b1.setOnClickListener(this);
Button b2=findViewById(R.id.b2);
b2.setOnClickListener(this);
Button b3=findViewById(R.id.b3);
b3.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.b1:
startActivity(new Intent(MainActivity.this,ResigerActivity.class));
break;
case R.id.b2:
startActivity(new Intent(MainActivity.this,SignActivity.class));
break;
case R.id.b3:
break;
}
}
}
package com.example.stusign;
import android.content.Context;
import android.content.DialogInterface;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.example.stusign.pojo.Stu;
import com.example.stusign.utils.StuUtils;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
public class ResigerActivity extends AppCompatActivity implements View.OnClickListener {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.stu_resiger);
Button button=findViewById(R.id.add);
button.setOnClickListener(this);
}
@Override
public void onClick(View view) {
EditText editText=findViewById(R.id.id);
int id=Integer.parseInt(editText.getText().toString());
editText=findViewById(R.id.name);
String name=editText.getText().toString();
editText=findViewById(R.id.phone);
String phone=editText.getText().toString();
editText=findViewById(R.id.unit);
String unit=editText.toString();
String sqlpath=getFilesDir()+"/stu.db";
SQLiteDatabase sqLiteDatabase=openOrCreateDatabase(sqlpath, Context.MODE_PRIVATE,null);
AlertDialog.Builder builder=new AlertDialog.Builder(this);
StuUtils stuUtils=StuUtils.getStuUtils(this);
stuUtils.onCreate(sqLiteDatabase);
builder.setTitle("注册结果");
if(stuUtils.add(sqLiteDatabase,new Stu(id,name,phone,unit))){
builder.setMessage("注册成功");
}
else{
builder.setMessage("注册失败");
}
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
finish();
}
});
builder.show();
}
}
package com.example.stusign.utils;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
import com.example.stusign.pojo.Stu;
public class StuUtils extends SQLiteOpenHelper {
private static StuUtils stuUtils;
private static final String Name="stu";
public static StuUtils getStuUtils(Context context){
if(stuUtils==null) stuUtils=new StuUtils(context);
return stuUtils;
}
private StuUtils(@Nullable Context context){
super(context,Name,null,1);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String sql="create table stu(" +
"id int(4) not null PRIMARY KEY," +
"`name` varchar(10) not null," +
"phone varchar(16) not null," +
"unit varchar(10) not null" +
")";
sqLiteDatabase.execSQL(sql);
}
public Boolean add(SQLiteDatabase sqLiteDatabase,Stu stu){
ContentValues values=new ContentValues();
values.put("id",stu.getId());
values.put("name",stu.getName());
values.put("phone",stu.getPhone());
values.put("unit", stu.getUnit());
return sqLiteDatabase.insert(Name,null,values)>0;
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
package com.example.stusign.utils;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.example.stusign.pojo.Sign;
public class SignUtils extends SQLiteOpenHelper {
private static SignUtils signUtils;
private static final String Name="sign";
public SignUtils getSignUtils(Context context){
if(signUtils==null) signUtils=new SignUtils(context);
return signUtils;
}
private SignUtils(Context context){
super(context,Name,null,1);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String sql="create table sign(" +
"id int(4) not null PRIMARY KEY," +
"`date` varchar(20) not null," +
"days int not null," +
"longdays int not null," +
"keywords varchar(50) not null," +
"summary varchar(500) not null)";
sqLiteDatabase.execSQL(sql);
}
public Boolean add(SQLiteDatabase sqLiteDatabase, Sign sign){
ContentValues values=new ContentValues();
values.put("id",sign.getId());
values.put("date",sign.getDate());
values.put("days",sign.getDays());
values.put("longdays",sign.getLongdays());
values.put("keywords",sign.getKeywords());
values.put("summary",sign.getSummary());
return sqLiteDatabase.insert(Name,null,values)>0;
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
标签:总结,public,values,import,今日,android,null,id From: https://www.cnblogs.com/liyiyang/p/17139405.html