Android-7

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:app="http://schemas.android.com/apk/res-auto"
 4     xmlns:tools="http://schemas.android.com/tools"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     android:orientation="vertical"
 8     tools:context=".MainActivity" >
 9 
10     <TextView
11         android:id="@+id/textView"
12         android:layout_width="match_parent"
13         android:layout_height="40dp"
14         android:background="#00BCD4"
15         android:gravity="center"
16         android:text="学生注册页面"
17         android:textSize="30sp"
18         android:textStyle="bold" />
19 
20     <LinearLayout
21         android:layout_width="match_parent"
22         android:layout_height="match_parent"
23         android:orientation="vertical">
24 
25         <TableLayout
26             android:layout_width="match_parent"
27             android:layout_height="wrap_content"
28             android:layout_margin="50dp">
29 
30             <TableRow
31                 android:layout_width="match_parent"
32                 android:layout_height="match_parent"
33                 android:gravity="center">
34 
35                 <TextView
36                     android:id="@+id/textView3"
37                     android:layout_width="wrap_content"
38                     android:layout_height="wrap_content"
39                     android:text="姓名:" />
40 
41                 <EditText
42                     android:id="@+id/e_name"
43                     android:layout_width="wrap_content"
44                     android:layout_height="wrap_content"
45                     android:ems="10"
46                     android:inputType="textPersonName" />
47             </TableRow>
48 
49             <TableRow
50                 android:layout_width="match_parent"
51                 android:layout_height="match_parent"
52                 android:gravity="center">
53 
54                 <TextView
55                     android:id="@+id/textView4"
56                     android:layout_width="wrap_content"
57                     android:layout_height="wrap_content"
58                     android:text="年龄:" />
59 
60                 <EditText
61                     android:id="@+id/e_age"
62                     android:layout_width="wrap_content"
63                     android:layout_height="wrap_content"
64                     android:ems="10"
65                     android:inputType="textPersonName" />
66             </TableRow>
67 
68         </TableLayout>
69 
70         <Button
71             android:id="@+id/button"
72             android:layout_width="150dp"
73             android:layout_height="wrap_content"
74             android:layout_gravity="center"
75             android:layout_margin="100dp"
76             android:gravity="center"
77             android:text="注册"
78             android:textSize="20sp"
79             android:textStyle="bold" />
80     </LinearLayout>
81 
82 </LinearLayout>
 1 package com.example.blj;
 2 
 3 import androidx.appcompat.app.AppCompatActivity;
 4 
 5 import android.content.ContentValues;
 6 import android.database.sqlite.SQLiteDatabase;
 7 import android.os.Bundle;
 8 import android.view.View;
 9 import android.widget.Button;
10 import android.widget.EditText;
11 import android.widget.TextView;
12 
13 public class MainActivity extends AppCompatActivity   {
14     private Button button;
15     private TextView name;
16     private EditText age;
17     @Override
18     protected void onCreate(Bundle savedInstanceState) {
19         super.onCreate(savedInstanceState);
20         setContentView(R.layout.activity_main);
21         button = (Button)findViewById(R.id.button);
22         name = (TextView)findViewById(R.id.e_name);
23         age = (EditText) findViewById(R.id.e_age);
24         button.setOnClickListener(new View.OnClickListener() {
25             @Override
26             public void onClick(View view) {
27                 insert(name.getText().toString(),age.getText().toString());
28             }
29         });
30     }
31     public void insert(String name,String age){
32         Student helper = new Student(this);
33         SQLiteDatabase db = helper.getWritableDatabase();
34         ContentValues values = new ContentValues();
35         values.put("name",name);
36         values.put("age",age);
37         long id = db.insert("stu",null,values);
38         db.close();
39     }
40 }
 1 package com.example.blj;
 2 
 3 import android.content.Context;
 4 import android.database.sqlite.SQLiteDatabase;
 5 import android.database.sqlite.SQLiteOpenHelper;
 6 
 7 import androidx.annotation.Nullable;
 8 
 9 public class Student extends SQLiteOpenHelper {
10     public Student(@Nullable Context context) {
11         super(context, "student.db", null, 3);
12     }
13 
14     @Override
15     public void onCreate(SQLiteDatabase db) {
16         String sql = "CREATE TABLE stu(s_id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(20),age INTEGER)";
17         db.execSQL(sql);
18     }
19 
20     @Override
21     public void onUpgrade(SQLiteDatabase db, int i, int i1) {
22         String sql = "CREATE TABLE bj(b_id INTEGER PRIMARY KEY AUTOINCREMENT,b_name VARCHAR(20))";
23         db.execSQL(sql);
24     }
25 }

 

 

 

 

 

posted on 2020-10-27 23:08  yunkuang  阅读(157)  评论(0编辑  收藏  举报