Android custom dialog to add data

Notes: practice

Table of contents

1. Two layout files (activity_main, dialog)

1.activity_main

 2.dialog

二、MainActivity

3. Results


1. Two layout files (activity_main, dialog)

1.activity_main

<?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/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:textSize="20dp"
        android:text="TextView" />

    <TextView
        android:id="@+id/number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:textSize="20dp"
        android:text="TextView" />

    <TextView
        android:id="@+id/age"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:textSize="20dp"
        android:text="TextView" />

    <Button
        android:id="@+id/add"
        android:layout_width="90dp"
        android:layout_height="60dp"
        android:layout_marginTop="80dp"
        android:layout_gravity="center_horizontal"
        android:backgroundTint="#53EEE1"
        android:text="添加"
        android:textSize="20dp" />
</LinearLayout>

 2.dialog

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/edName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="请输入姓名"
        android:inputType="textPersonName" />

    <EditText
        android:id="@+id/edNum"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="请输入学号"
        android:inputType="textPersonName" />

    <EditText
        android:id="@+id/edAge"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="请输入年龄"
        android:inputType="textPersonName" />
</LinearLayout>

二、MainActivity

public class MainActivity extends AppCompatActivity {

    private Button btnAdd;
    private TextView name,number,age;

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

        name=findViewById(R.id.name);
        number=findViewById(R.id.number);
        age=findViewById(R.id.age);
        btnAdd=findViewById(R.id.add);
//        添加按钮
        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                add();
            }
        });

    }
//    添加
    private void add(){
        AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("请输入信息");
        //  加载一个xml的布局文件作为一个View对象
        View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog,null);
        builder.setView(view);
        final EditText edName=view.findViewById(R.id.edName);
        final EditText edNum=view.findViewById(R.id.edNum);
        final EditText edAge=view.findViewById(R.id.edAge);
//        设置取消键
       builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
           @Override
           public void onClick(DialogInterface dialogInterface, int i) {

           }
       });
//       设置确定键
       builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
           @Override
           public void onClick(DialogInterface dialogInterface, int i) {
//               将结果显示到textView上
               name.setText("姓名:"+edName.getText());
               number.setText("学号:"+edNum.getText());
               age.setText("年龄:"+edAge.getText());
           }
       });
       builder.show();
    }
}

3. Results

 

Guess you like

Origin blog.csdn.net/qq_47229902/article/details/125075330