安卓作業----慕課移動應用開發作業22之ContentProvider,ContentResolver與SqliteOpenHelper簡單應用

本篇做了一個ContentProvider、ContentResolver、SqliteOpenHelper的簡單應用,通過ContentProvider提供數據,通過ContentPResolver讀取數據。
同時這也是中國大學慕課移動終端應用開發的網課作業22

原題目要求:
(1)創建2個項目,一個ProviderApp用於提供數據,一個ResoverApp用於使用數據,數據源使用SQLite數據庫。
(2)自定義ContentProvider。
(3)自定義SQLiteOpenHelper,創建數據庫與表。
(4)對數據庫的增、刪、改、查操作。

說明

1.兩個應用,ProviderApp提供數據,ResoverApp使用數據。
2.由於時間問題,我沒有做一些判錯處理,測試的時候儘量跟隨提示輸入,進行正確操作,錯誤的操作如查詢爲空的時候會閃退。
3.修改數據功能是根據id進行修改的
3.有關ContentProvider方面知識,我建議大家結合網課和此篇博客學習。

演示視頻

由於gif圖片大小有限制,我只演示部分功能
在這裏插入圖片描述

目錄結構

ProviderApp

在這裏插入圖片描述

ResoverApp

在這裏插入圖片描述

代碼部分

ProviderApp

1.配置文件 AndroidManifest.xml

添加如下部分

 <provider
	  android:name=".provider.WordContentProvider"
	  android:authorities="com.example.providerapp.WordContentProvider"
	  android:enabled="true"
	  android:exported="true"></provider>

2.WordCursorWrapper.java
public class WordCursorWrapper extends CursorWrapper {
    /**
     * Creates a cursor wrapper.
     *
     * @param cursor The underlying cursor to wrap.
     */
    public WordCursorWrapper(Cursor cursor) {
        super(cursor);
    }

    public Word getWord(){
        //獲取數據
        int id = getInt(getColumnIndex(WordDbSchema.WordTable.Columns.ID));
        String english = getString(getColumnIndex(WordDbSchema.WordTable.Columns.ENGLISH));
        String chinese = getString(getColumnIndex(WordDbSchema.WordTable.Columns.CHINESE));
        String description = getString(getColumnIndex(WordDbSchema.WordTable.Columns.DESCRIPTION));

        return new Word(id,english,chinese,description);
    }
}

3.WordDbSchema.java
public class WordDbSchema {
    public static final class WordTable{
        //定義表名
        public static final String TABLE_NAME = "words";

        //定義數據表字段
        public static final class Columns{
            public static final String ID = "id";
            public static final String ENGLISH = "english";
            public static final String CHINESE = "chinese";
            public static final String DESCRIPTION = "description";
        }
    }
}
4.WordSqlHelper.java
public class WordSqlHelper extends SQLiteOpenHelper {
    private static final int VERSION = 1;//定義版本
    private static final String DATABASE_NAME = "course22DataBase";

    public WordSqlHelper(@Nullable Context context) {
        super(context, DATABASE_NAME, null, VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table "+ WordDbSchema.WordTable.TABLE_NAME+
                "("+"id integer primary key autoincrement,"+
                WordDbSchema.WordTable.Columns.ENGLISH+","+
                WordDbSchema.WordTable.Columns.CHINESE+","+
                WordDbSchema.WordTable.Columns.DESCRIPTION+ ")"
        );
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //不更新
    }
}
5.Word.java
public class Word {
    private int id;
    private String english;
    private String chinese;
    private String description;

    public Word() {
    }

    public Word(int id, String english, String chinese, String description) {
        this.id = id;
        this.english = english;
        this.chinese = chinese;
        this.description = description;
    }

    public Word(String english, String chinese, String description) {
        this.english = english;
        this.chinese = chinese;
        this.description = description;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "單詞ID:" + id + " 英文:" + english + " 中文:" + chinese + " 描述:" + description;
    }

    public String getEnglish() {
        return english;
    }

    public void setEnglish(String english) {
        this.english = english;
    }

    public String getChinese() {
        return chinese;
    }

    public void setChinese(String chinese) {
        this.chinese = chinese;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

6.WordContentProvider.java
public class WordContentProvider extends ContentProvider {
    private Context mContext;
    private SQLiteDatabase mDatabase;

    //創建authority,授權
    public static final String AUTHORITY = "com.example.providerapp.WordContentProvider";
    //創建UriMatcher對象
    private static UriMatcher sUriMatcher;

    //靜態代碼塊
    static {
        //實例化UriMatcher對象
        sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

        //匹配uri
        //word是表名
        sUriMatcher.addURI(AUTHORITY, "word", 1);
        //匹配數字
        sUriMatcher.addURI(AUTHORITY, "word/#", 2);
        //匹配文本
        sUriMatcher.addURI(AUTHORITY, "word/*", 3);
        //匹配文本
        sUriMatcher.addURI(AUTHORITY, "word/*/*", 4);

    }

    @Override
    public boolean onCreate() {
        //在這裏打開對應數據庫
        mContext = this.getContext();
        mDatabase = new WordSqlHelper(mContext).getWritableDatabase();

        return true;
    }

    public WordContentProvider() { }

    /**
     * 這裏提供了三種刪除方式,通過id,通過名字,啥也沒有就是全刪除
     * */
    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
       int code = sUriMatcher.match(uri);
       int result = 0;
       switch (code){
           case UriMatcher.NO_MATCH:
               break;
           case 1://刪除表中所有記錄
               result = mDatabase.delete(WordDbSchema.WordTable.TABLE_NAME,null,null);
               break;
           case 2://投機取巧了一下
               // 按id刪除
               result = mDatabase.delete(WordDbSchema.WordTable.TABLE_NAME,WordDbSchema.WordTable.Columns.ID+" = ? ",
                       new String[]{ContentUris.parseId(uri) + ""});
               break;
           case 3:
               //按english刪除
               result = mDatabase.delete(WordDbSchema.WordTable.TABLE_NAME,WordDbSchema.WordTable.Columns.ENGLISH+" = ? ",
                       new String[]{uri.getPathSegments().get(1)});
               break;
       }

       return result;
    }

    @Override
    public String getType(Uri uri) {
        // TODO: Implement this to handle requests for the MIME type of the data
        // at the given URI.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    /**
     * 插入數據,數據一開始就得values
     * */
    @Override
    public Uri insert(Uri uri, ContentValues values) {
        int code = sUriMatcher.match(uri);
        Uri result = null;
        switch (code){
            case 1://插入
                long index = mDatabase.insert(WordDbSchema.WordTable.TABLE_NAME,null,values);
                if (index > 0){
                    //插入成功,在前面已經有的uri後面追加index
                    result = ContentUris.withAppendedId(uri,index);
                    //通知數據已經發生改變
                    getContext().getContentResolver().notifyChange(result,null);
                    return result;
                }
                break;
        }
        return null;
    }


    /**
     * 查找單詞信息
     * */
    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
        int code = sUriMatcher.match(uri);
        Cursor result = null;

        switch (code){
            case 1://查詢所有記錄
                result = mDatabase.query(WordDbSchema.WordTable.TABLE_NAME,
                        projection,selection,selectionArgs,null,null,sortOrder);
                break;
            case 2://根據id查詢記錄
                result = mDatabase.query(WordDbSchema.WordTable.TABLE_NAME,
                        projection,WordDbSchema.WordTable.Columns.ID+" = ? ",
                        new String[]{ ContentUris.parseId(uri) + "" },null,null,sortOrder);
                break;
            case 3://根據English字段查詢
                result = mDatabase.query(WordDbSchema.WordTable.TABLE_NAME,
                        projection,WordDbSchema.WordTable.Columns.ENGLISH + " = ? ",
                        new String[]{uri.getPathSegments().get(1)},null,null,sortOrder);
                break;
//            case 4://根據指定的字段名查詢記錄,第一個是字段名,第二個是參數
//                result = mDatabase.query(WordDbSchema.WordTable.TABLE_NAME,
//                        projection,uri.getPathSegments().get(1) + " = ? ",
//                        new String[]{uri.getPathSegments().get(2)},null,null,sortOrder);
//                break;
        }
        return result;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        int code = sUriMatcher.match(uri);
        int result = 0;
        switch (code) {
            case 2://根據指定id更新記錄
                result = mDatabase.update(WordDbSchema.WordTable.TABLE_NAME,values,
                        WordDbSchema.WordTable.Columns.ID+" = ? ",new String[]{ContentUris.parseId(uri) + ""});
        }
        return result;
    }


    //私有方法,返回一個ContentValues對象
    private ContentValues getContentValues(Word word){
        ContentValues values = new ContentValues();
        //添加鍵值對
        values.put(WordDbSchema.WordTable.Columns.ENGLISH,word.getEnglish());
        values.put(WordDbSchema.WordTable.Columns.CHINESE,word.getChinese());
        values.put(WordDbSchema.WordTable.Columns.DESCRIPTION,word.getDescription());
        return values;
    }


}
7.MainActivity.java
public class MainActivity extends AppCompatActivity {
    private Button mButton;
    private TextView mTextView;

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

        mButton = findViewById(R.id.update);
        mTextView = findViewById(R.id.show);

        /**
         * 簡單更新數據
         * */
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //此uri用於查詢所有word
                Uri uri = Uri.parse("content://com.example.providerapp.WordContentProvider/word");
                ContentResolver contentResolver = getContentResolver();
                Cursor cursor = contentResolver.query(uri,
                        new String[]{WordDbSchema.WordTable.Columns.ID,
                                WordDbSchema.WordTable.Columns.ENGLISH,
                                WordDbSchema.WordTable.Columns.CHINESE,
                                WordDbSchema.WordTable.Columns.DESCRIPTION,}, null, null, null);

                ArrayList<Word> words = getWords(cursor);
                mTextView.setText(getString(words));


            }
        });

    }

    /**
     * 獲取arrayList對象
     * */
    private ArrayList<Word> getWords(Cursor cursor){
        ArrayList<Word> words = new ArrayList<>();
        WordCursorWrapper wrapper = new WordCursorWrapper(cursor);

        try {
            wrapper.moveToFirst();
            while (!wrapper.isAfterLast()){
                words.add(wrapper.getWord());
                wrapper.moveToNext();
            }
        } finally {
            wrapper.close();
        }
        return words;
    }

    /**
     * 將列表展開成字符串
     * */
    private String getString(ArrayList<Word> words){
        String res = "";
        for (Word word:words){
            res += word.toString()+"\n";
        }
        return res;
    }



}
8.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:orientation="vertical">
    <TextView
        android:id="@+id/show"
        android:textSize="20dp"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
    <Button
        android:layout_gravity="bottom"
        android:id="@+id/update"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="更新數據"/>

</LinearLayout>

ResoverApp

1.WordCursorWrapper.java
public class WordCursorWrapper extends CursorWrapper {
    /**
     * Creates a cursor wrapper.
     *
     * @param cursor The underlying cursor to wrap.
     */
    public WordCursorWrapper(Cursor cursor) {
        super(cursor);
    }

    public Word getWord(){
        //獲取數據
        int id = getInt(getColumnIndex(WordDbSchema.WordTable.Columns.ID));
        String english = getString(getColumnIndex(WordDbSchema.WordTable.Columns.ENGLISH));
        String chinese = getString(getColumnIndex(WordDbSchema.WordTable.Columns.CHINESE));
        String description = getString(getColumnIndex(WordDbSchema.WordTable.Columns.DESCRIPTION));

        return new Word(id,english,chinese,description);
    }
}
2.WordDbSchema.java
public class WordDbSchema {
    public static final class WordTable{
        //定義表名
        public static final String TABLE_NAME = "words";

        //定義數據表字段
        public static final class Columns{
            public static final String ID = "id";
            public static final String ENGLISH = "english";
            public static final String CHINESE = "chinese";
            public static final String DESCRIPTION = "description";
        }
    }
}

3.Word.java
public class Word {
    private int id;
    private String english;
    private String chinese;
    private String description;

    public Word() {
    }

    public Word(int id, String english, String chinese, String description) {
        this.id = id;
        this.english = english;
        this.chinese = chinese;
        this.description = description;
    }

    public Word(String english, String chinese, String description) {
        this.english = english;
        this.chinese = chinese;
        this.description = description;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "單詞ID:" + id + " 英文:" + english + " 中文:" + chinese + " 描述:" + description;
    }

    public String getEnglish() {
        return english;
    }

    public void setEnglish(String english) {
        this.english = english;
    }

    public String getChinese() {
        return chinese;
    }

    public void setChinese(String chinese) {
        this.chinese = chinese;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}
4.MainActivity.java
public class MainActivity extends AppCompatActivity {

    private EditText mEditTextID,mEditTextEnglish,mEditTextChinese,mEditTextDescription;
    private Button mButtonAdd,mButtonUpdate,mButtonDeleteAll,mButtonDeleteByEnglish,mButtonDeleteByID,
    mButtonFindAll,mButtonFindByEnglish,mButtonFindByID;
    private TextView mTextView;

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

        initView();

        mButtonAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String english = mEditTextEnglish.getText().toString();
                String chinese = mEditTextChinese.getText().toString();
                String description = mEditTextDescription.getText().toString();

                ContentValues values = new ContentValues();
                values.put("english",english);
                values.put("chinese",chinese);
                values.put("description",description);

                Uri uri = Uri.parse("content://com.example.providerapp.WordContentProvider/word");
                ContentResolver contentResolver = getContentResolver();

                Uri result = contentResolver.insert(uri,values);
                if(result!=null){
                    Toast.makeText(MainActivity.this,
                            "插入成功!",Toast.LENGTH_LONG).show();
                }else {
                    Toast.makeText(MainActivity.this,"插入失敗!",
                            Toast.LENGTH_LONG).show();
                }
                cleanEditText();
            }
        });

        mButtonDeleteAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri uri = Uri.parse("content://com.example.providerapp.WordContentProvider/word");
                ContentResolver contentResolver = getContentResolver();

                int result = contentResolver.delete(uri,null,null);
                if (result > 0){
                    Toast.makeText(MainActivity.this,
                            "刪除成功!",Toast.LENGTH_LONG).show();
                }else {
                    Toast.makeText(MainActivity.this,
                            "刪除失敗!",Toast.LENGTH_LONG).show();
                }
                cleanEditText();
            }
        });

        mButtonDeleteByID.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int id = Integer.parseInt(mEditTextID.getText().toString());

                Uri uri = Uri.parse("content://com.example.providerapp.WordContentProvider/word/"+id);
                ContentResolver contentResolver = getContentResolver();
                int result = contentResolver.delete(uri,null,null);
                if (result > 0){
                    Toast.makeText(MainActivity.this,
                            "刪除成功!",Toast.LENGTH_LONG).show();
                }else {
                    Toast.makeText(MainActivity.this,
                            "刪除失敗!",Toast.LENGTH_LONG).show();
                }
                cleanEditText();
            }
        });

        mButtonDeleteByEnglish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String english = mEditTextEnglish.getText().toString();

                Uri uri = Uri.parse("content://com.example.providerapp.WordContentProvider/word/"+english);
                ContentResolver contentResolver = getContentResolver();
                int result = contentResolver.delete(uri,null,null);
                if (result > 0){
                    Toast.makeText(MainActivity.this,
                            "刪除成功!",Toast.LENGTH_LONG).show();
                }else {
                    Toast.makeText(MainActivity.this,
                            "刪除失敗!",Toast.LENGTH_LONG).show();
                }
                cleanEditText();
            }
        });

        mButtonUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String english = mEditTextEnglish.getText().toString();
                String chinese = mEditTextChinese.getText().toString();
                String description = mEditTextDescription.getText().toString();

                ContentValues values = new ContentValues();
                values.put(WordDbSchema.WordTable.Columns.ENGLISH,english);
                values.put(WordDbSchema.WordTable.Columns.CHINESE,chinese);
                values.put(WordDbSchema.WordTable.Columns.DESCRIPTION,description);

                String id = mEditTextID.getText().toString();

                Uri uri = Uri.parse("content://com.example.providerapp.WordContentProvider/word/"+id);
                ContentResolver contentResolver = getContentResolver();
                int result = contentResolver.update(uri,values,null,null);

                if (result > 0){
                    Toast.makeText(MainActivity.this,
                            "更新成功!",Toast.LENGTH_LONG).show();
                }else {
                    Toast.makeText(MainActivity.this,
                            "更新失敗!",Toast.LENGTH_LONG).show();
                }
                cleanEditText();
            }
        });

        mButtonFindAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //此uri用於查詢所有word
                Uri uri = Uri.parse("content://com.example.providerapp.WordContentProvider/word");
                ContentResolver contentResolver = getContentResolver();
                Cursor cursor = contentResolver.query(uri,
                        new String[]{WordDbSchema.WordTable.Columns.ID,
                                WordDbSchema.WordTable.Columns.ENGLISH,
                                WordDbSchema.WordTable.Columns.CHINESE,
                                WordDbSchema.WordTable.Columns.DESCRIPTION,}, null, null, null);

                ArrayList<Word> words = getWords(cursor);
                mTextView.setText(getString(words));
                cleanEditText();
            }
        });

        mButtonFindByEnglish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String english = mEditTextEnglish.getText().toString();

                Uri uri = Uri.parse("content://com.example.providerapp.WordContentProvider/word/"+english);
                ContentResolver contentResolver = getContentResolver();

                Cursor cursor = contentResolver.query(uri,
                        new String[]{WordDbSchema.WordTable.Columns.ID,
                                WordDbSchema.WordTable.Columns.ENGLISH,
                                WordDbSchema.WordTable.Columns.CHINESE,
                                WordDbSchema.WordTable.Columns.DESCRIPTION,}, null, null, null);

                ArrayList<Word> words = getWords(cursor);
                mTextView.setText(getString(words));
                cleanEditText();
            }
        });

        mButtonFindByID.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int id = Integer.parseInt(mEditTextID.getText().toString());

                Uri uri = Uri.parse("content://com.example.providerapp.WordContentProvider/word/"+id);
                ContentResolver contentResolver = getContentResolver();

                Cursor cursor = contentResolver.query(uri,
                        new String[]{WordDbSchema.WordTable.Columns.ID,
                                WordDbSchema.WordTable.Columns.ENGLISH,
                                WordDbSchema.WordTable.Columns.CHINESE,
                                WordDbSchema.WordTable.Columns.DESCRIPTION,}, null, null, null);

                ArrayList<Word> words = getWords(cursor);
                mTextView.setText(getString(words));
                cleanEditText();
            }
        });


    }

    /**
     * 獲取arrayList對象
     * */
    private ArrayList<Word> getWords(Cursor cursor){
        ArrayList<Word> words = new ArrayList<>();
        WordCursorWrapper wrapper = new WordCursorWrapper(cursor);

        try {
            wrapper.moveToFirst();
            while (!wrapper.isAfterLast()){
                words.add(wrapper.getWord());
                wrapper.moveToNext();
            }
        } finally {
            wrapper.close();
        }
        return words;
    }

    /**
     * 將列表展開成字符串
     * */
    private String getString(ArrayList<Word> words){
        String res = "";
        for (Word word:words){
            res += word.toString()+"\n";
        }
        return res;
    }

    private void cleanEditText(){
        mEditTextID.setText("");
        mEditTextChinese.setText("");
        mEditTextEnglish.setText("");
        mEditTextDescription.setText("");
    }

    private void initView(){
        mEditTextID = findViewById(R.id.edit_id);
        mEditTextEnglish = findViewById(R.id.edit_english);
        mEditTextChinese = findViewById(R.id.edit_chinese);
        mEditTextDescription = findViewById(R.id.edit_description);

        mButtonAdd = findViewById(R.id.button_add);
        mButtonUpdate = findViewById(R.id.button_update);

        mButtonDeleteAll = findViewById(R.id.button_delete_all);
        mButtonDeleteByEnglish = findViewById(R.id.button_delete_by_english);
        mButtonDeleteByID = findViewById(R.id.button_delete_by_id);

        mButtonFindAll = findViewById(R.id.button_find_all);
        mButtonFindByEnglish = findViewById(R.id.button_find_by_english);
        mButtonFindByID = findViewById(R.id.button_find_by_id);

        mTextView = findViewById(R.id.show);
    }
}
5.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:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:background="#CFE7FA">

        <LinearLayout
            android:layout_centerInParent="true"
            android:layout_width="300dp"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1">
                <TextView
                    android:text="英文:"
                    android:layout_marginLeft="10dp"
                    android:layout_gravity="center"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <EditText
                    android:id="@+id/edit_english"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="match_parent"
                    android:textSize="12dp"
                    android:hint="添加或按英文查找、刪除時填寫"/>

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1">
                <TextView
                    android:text="中文:"
                    android:layout_marginLeft="10dp"
                    android:layout_gravity="center"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <EditText
                    android:id="@+id/edit_chinese"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="match_parent"
                    android:textSize="12dp"
                    android:hint="添加數據時填寫"/>

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1">
                <TextView
                    android:text="描述:"
                    android:layout_marginLeft="10dp"
                    android:layout_gravity="center"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <EditText
                    android:id="@+id/edit_description"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="match_parent"
                    android:textSize="12dp"
                    android:hint="添加數據時填寫"/>

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1">
                <TextView
                    android:text=" I D :"
                    android:layout_marginLeft="10dp"
                    android:layout_gravity="center"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <EditText
                    android:id="@+id/edit_id"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="match_parent"
                    android:textSize="12dp"
                    android:hint="按ID查詢、修改、刪除時填寫"/>

            </LinearLayout>
        </LinearLayout>
    </RelativeLayout>
    <TextView
        android:id="@+id/show"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_marginTop="10dp"
            android:layout_height="0dp"
            android:layout_weight="1">
            <Button
                android:id="@+id/button_add"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#BDF8F8"
                android:text="添加數據"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"/>
        </LinearLayout>
        <LinearLayout
            android:layout_marginTop="10dp"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

            <Button
                android:id="@+id/button_delete_all"
                android:text="全部刪除"
                android:layout_marginLeft="5dp"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="3"
                android:background="#ECF0DD"/>
            <Button
                android:id="@+id/button_delete_by_id"
                android:text="按ID刪除"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="3"
                android:background="#ECF0DD"/>
            <Button
                android:id="@+id/button_delete_by_english"
                android:text="按英文刪除"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="3"
                android:background="#ECF0DD"
                android:layout_marginRight="5dp"/>

        </LinearLayout>
        <LinearLayout
            android:layout_marginTop="10dp"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            >
            <Button
                android:id="@+id/button_update"
                android:background="#D4F5C1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="修改數據"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"/>
        </LinearLayout>
        <LinearLayout
            android:layout_marginTop="10dp"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            >
            <Button
                android:id="@+id/button_find_all"
                android:text="查詢全部"
                android:layout_marginLeft="5dp"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="3"
                android:background="#EEE0F1" />
            <Button
                android:id="@+id/button_find_by_id"
                android:text="按ID查詢"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="3"
                android:background="#EEE0F1"/>
            <Button
                android:id="@+id/button_find_by_english"
                android:text="按英文查詢"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="3"
                android:background="#EEE0F1"
                android:layout_marginRight="5dp"/>
        </LinearLayout>


    </LinearLayout>

</LinearLayout>

總結

如果有什麼問題或改進方案,請私信聯繫我或者在評論區留言
碼字不易,若有幫助,給個關注和讚唄

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章