码迷,mamicode.com
首页 > 移动开发 > 详细

Android11

时间:2020-12-25 11:54:21      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:idg   primary   this   extends   textview   content   div   roi   xmlns   

<?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" >

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="#00BCD4"
        android:gravity="center"
        android:text="学生注册页面"
        android:textSize="30sp"
        android:textStyle="bold" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="50dp">

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center">

                <TextView
                    android:id="@+id/textView3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="姓名:" />

                <EditText
                    android:id="@+id/e_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:ems="10"
                    android:inputType="textPersonName" />
            </TableRow>

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center">

                <TextView
                    android:id="@+id/textView4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="年龄:" />

                <EditText
                    android:id="@+id/e_age"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:ems="10"
                    android:inputType="textPersonName" />
            </TableRow>

        </TableLayout>

        <Button
            android:id="@+id/button"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="100dp"
            android:gravity="center"
            android:text="注册"
            android:textSize="20sp"
            android:textStyle="bold" />
    </LinearLayout>

</LinearLayout>
package com.example.huji;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity   {
    private Button button;
    private TextView name;
    private EditText age;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button)findViewById(R.id.button);
        name = (TextView)findViewById(R.id.e_name);
        age = (EditText) findViewById(R.id.e_age);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                insert(name.getText().toString(),age.getText().toString());
            }
        });
    }
    public void insert(String name,String age){
        Student helper = new Student(this);
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("name",name);
        values.put("age",age);
        long id = db.insert("stu",null,values);
        db.close();
    }
}
package com.example.huji;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity   {
    private Button button;
    private TextView name;
    private EditText age;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button)findViewById(R.id.button);
        name = (TextView)findViewById(R.id.e_name);
        age = (EditText) findViewById(R.id.e_age);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                insert(name.getText().toString(),age.getText().toString());
            }
        });
    }
    public void insert(String name,String age){
        Student helper = new Student(this);
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("name",name);
        values.put("age",age);
        long id = db.insert("stu",null,values);
        db.close();
    }
}
package com.example.huji;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

public class Student extends SQLiteOpenHelper {
    public Student(@Nullable Context context) {
        super(context, "student.db", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "CREATE TABLE stu(s_id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(20),age INTEGER)";
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }
}

技术图片

Android11

标签:idg   primary   this   extends   textview   content   div   roi   xmlns   

原文地址:https://www.cnblogs.com/1206wang/p/14163595.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!