Android development and learning-day6

Write in front

What I did today is the address book: the list of contacts, the addition of contacts, import dependencies and database operations

1.ui interface

View the content of the page, write the layout

First page

【activity_main】 ```xml <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:padding="4dp"
    android:background="#f8f8f8"
    android:orientation="horizontal"
    >
    <TextView
        android:layout_weight="8"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:gravity="center|left"
        android:textSize="24sp"
        android:textColor="@color/black"
        android:text="@string/contacts"/>
    <TextView
        android:id="@+id/tv_add"
        android:layout_weight="2"
        android:layout_width="0dp"
        android:gravity="center|right"
        android:layout_height="match_parent"
        android:textSize="22sp"
        android:textColor="@color/black"
        android:text="@string/add"
        />
</LinearLayout>

<LinearLayout
    android:layout_weight="8"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:orientation="vertical"
    android:padding="10dp"
>
    <LinearLayout
        android:background="@drawable/boder_so"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:padding="10dp">
        <ImageView
            android:src="@drawable/so"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"/>
        <EditText
            android:id="@+id/edt_find_contact"
            android:layout_width="match_parent"
            android:background="@null"
            android:layout_height="match_parent"/>
    </LinearLayout>
    <ListView
        android:id="@+id/lv_contacts"
        android:layout_weight="9"
        android:layout_width="match_parent"
        android:layout_height="0dp"/>
</LinearLayout>
effect:

Second page

【Activity_add】 ```xml <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:background="#f2f2f2"
    android:orientation="horizontal"
    >
    <ImageView
        android:id="@+id/cancel"
        android:src="@drawable/cha"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:padding="6dp"
        />
    <TextView
        android:layout_weight="6"
        android:layout_width="0dp"
        android:text="@string/add_contacts"
        android:textSize="24sp"
        android:textColor="@color/black"
        android:gravity="center|left"
        android:layout_height="match_parent"/>
    <ImageView
        android:id="@+id/ok"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:src="@drawable/gou"
        android:padding="6dp"
        />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="8"
    android:gravity="center|top"
    android:orientation="vertical"
    android:padding="50dp"
    android:paddingTop="10dp">

    <LinearLayout
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@drawable/border_round"
        android:gravity="center">

        <ImageView
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:src="@drawable/camera" />
    </LinearLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="保存至:手机"
        android:textColor="@color/black"
        android:textSize="20sp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:paddingBottom="20dp"
        android:text="仅保存在手机,开启云空间可同步"
        android:textSize="18sp" />

    <EditText
        android:id="@+id/edt_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:lines="1"
        android:hint="姓名" />

    <EditText
        android:id="@+id/edt_company"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:lines="1"
        android:hint="公司" />

    <EditText
        android:id="@+id/edt_tel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:lines="1"
        android:hint="电话号码" />
</LinearLayout>
```
effect:

2. Implement the button

【MainActivity.java】· ```java package com.example.task3;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private TextView tvAdd;
private EditText edtSo;
private ListView lvContacts;

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

    tvAdd = findViewById(R.id.tv_add);
    edtSo = findViewById(R.id.edt_find_contact);
    lvContacts = findViewById(R.id.lv_contacts);

    edtSo.clearFocus();

    tvAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, AddActivity.class);

            startActivity(intent);
        }
    });
}

【AddActivity.java】

package com.example.task3;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class AddActivity extends AppCompatActivity {
    
    

    private ImageView ok;
    private ImageView cancel;
    private EditText edtName;
    private EditText edtCompany;
    private EditText edtTel;

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

        ok = findViewById(R.id.ok);
        cancel = findViewById(R.id.cancel);
        edtName = findViewById(R.id.edt_name);
        edtCompany = findViewById(R.id.edt_company);
        edtTel = findViewById(R.id.edt_tel);

        ok.setOnClickListener(new View.OnClickListener() {
    
    
            @Override
            public void onClick(View v) {
    
    

                Toast.makeText(AddActivity.this, "添加" + edtName.getText().toString().trim() + "成功"
                        ,Toast.LENGTH_LONG).show();

                Intent intent = new Intent(AddActivity.this, MainActivity.class);

                startActivity(intent);
            }
        });
        cancel.setOnClickListener(new View.OnClickListener() {
    
    
            @Override
            public void onClick(View v) {
    
    
                Intent intent = new Intent(AddActivity.this, MainActivity.class);

                startActivity(intent);
            }
        });

    }
}
effect:

achieve

3. Import dependencies

  1. Download the package that needs to be imported:
    ormlite website
    Download: [ormlite-android:5.0][ormlite-core:5.0]

  2. Put the following package into the lib directory:

3. Import in build.gradle:

implementation 'com.j256.ormlite:ormlite-android:5.0'
implementation 'com.j256.ormlite:ormlite-core:5.0'

to sum up:


Today, I just implemented the UI and buttons of the address book. I still don’t know how to read the tutorial on the database. I have to find the tutorial again tomorrow to learn.

Guess you like

Origin blog.csdn.net/weixin_45936162/article/details/112674820