1. 程式人生 > 其它 >sprites 精靈圖(雪碧圖)

sprites 精靈圖(雪碧圖)

activity_main.xml

<?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:background="@drawable/bg"
    android:orientation="vertical"
    android:padding
="16dp" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="130dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text
="姓 名 :" android:textSize="18sp" /> <EditText android:id="@+id/et_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="請輸入姓名" android:textSize="16sp" /> </LinearLayout> <LinearLayout android:layout_width
="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="年 齡:" android:textSize="18sp" /> <EditText android:id="@+id/et_age" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="請輸入年齡" android:textSize="16sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/btn_add" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginRight="2dp" android:layout_weight="1" android:background="#B9B9FF" android:onClick="add" android:text="新增" android:textSize="18sp" /> <Button android:id="@+id/btn_query" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginRight="2dp" android:layout_weight="1" android:background="#DCB5FF" android:onClick="search" android:text="查詢" android:textSize="18sp" /> <Button android:id="@+id/btn_update" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginRight="2dp" android:layout_weight="1" android:background="#E6CAFF" android:onClick="update" android:text="修改" android:textSize="18sp" /> <Button android:id="@+id/btn_delete" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#ACD6FF" android:onClick="delete" android:text="刪除" android:textSize="18sp" /> </LinearLayout> <ScrollView android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/tv_show" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="25dp" android:textSize="20sp" /> </ScrollView> </LinearLayout>

MainActivity.java

package com.example.myaddressbook;

import androidx.appcompat.app.AppCompatActivity;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void add(View v){
        StuOpenHelper helper=new StuOpenHelper(this);
        SQLiteDatabase db=helper.getWritableDatabase();
        String name=((EditText)findViewById(R.id.et_name)).getText().toString();
        int age=Integer.parseInt(((EditText)findViewById(R.id.et_age)).getText().toString());

        db.execSQL("insert into stuinfo(name,age) values(?,?)",new Object[]{
                name,age
        });
        Toast.makeText(this,"ok",Toast.LENGTH_LONG).show();
    }

    public void delete(View view) {
        StuOpenHelper helper = new StuOpenHelper(this);
        SQLiteDatabase db = helper.getWritableDatabase();
        db.execSQL("delete from stuinfo where _id=?",new Object[]{2});
        Toast.makeText(this,"修改成功",Toast.LENGTH_LONG).show();
    }

    public void update(View view) {
        StuOpenHelper helper = new StuOpenHelper(this);
        SQLiteDatabase db = helper.getWritableDatabase();
        db.execSQL("update stuinfo set name=? where _id=?",new Object[]{
                "Mickey",3
        });
        Toast.makeText(this,"修改成功",Toast.LENGTH_LONG).show();
    }

    public void search(View view) {
        StuOpenHelper helper = new StuOpenHelper(this);
        SQLiteDatabase db = helper.getWritableDatabase();
        String s="";
        Cursor cursor=db.rawQuery("select * from stuinfo",null);
        if(cursor.getCount() !=0){
            while(cursor.moveToNext()){
                s +=cursor.getInt(0)+"     "+cursor.getString(1)+"    "+cursor.getInt(2)+"\n";
            }
        }
        ((TextView)(findViewById(R.id.tv_show))).setText(s);
    }
}

StuOpenHelper.java

package com.example.myaddressbook;

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

import androidx.annotation.Nullable;

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

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table stuinfo(_id integer primary key autoincrement,name varchar(20),age integer)");
    }

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

    }
}