Android studio development three: the addition and use of common controls (radio buttons, multiple choice buttons, Spinner, dialog boxes, etc.)

The addition and use of common controls (radio buttons, multiple choice buttons, Spinner, dialog boxes, etc.)


Not much to say, directly upload the source code and screenshots
to MainActivity.java

package com.example.calculator;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Stack;

public class MainActivity extends AppCompatActivity {
    
    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate (savedInstanceState);
        setContentView (R.layout.activity_main);
    }
    public void myclick(View v){
    
    
        switch (v.getId ()){
    
    
            case R.id.btn_1:
                showSingleDialog();
                break;
            case R.id.btn_2:
                showMultiDialog();
                break;
            case R.id.btn_3:
                showNormalDialog2();
                break;
            case R.id.btn_4:
                showListDialog();
                break;
            case R.id.btn_5:
                showWaitDialog();
                break;
            case R.id.btn_6:
                showProgressDialog();
                break;
            case R.id.btn_7:
                showNormalDialog1();
                break;
        }
    }

    private void showNormalDialog1() {
    
    
        AlertDialog.Builder dialog = new AlertDialog.Builder (this);
        dialog.setTitle ("警告!").setMessage ("此行为即将退出程序");
                dialog.setPositiveButton ("确定", new DialogInterface.OnClickListener () {
    
    
            @Override
            public void onClick(DialogInterface dialog, int which) {
    
    
                MainActivity.this.finish ();
            }
        });
        dialog.setNegativeButton ("取消",null);
        dialog.show ();
    }

    private void showNormalDialog2(){
    
    
        AlertDialog dialog2 = new AlertDialog.Builder (this).create ();
        dialog2.setTitle ("售后评价");
        dialog2.setMessage ("本次的服务如何?");
        dialog2.setButton (DialogInterface.BUTTON_POSITIVE, "好", new DialogInterface.OnClickListener () {
    
    
            @Override
            public void onClick(DialogInterface dialog, int which) {
    
    
                Toast.makeText (MainActivity.this,"好",Toast.LENGTH_LONG).show ();
            }
        });
        dialog2.setButton (DialogInterface.BUTTON_NEGATIVE, "差", new DialogInterface.OnClickListener () {
    
    
            @Override
            public void onClick(DialogInterface dialog, int which) {
    
    
                Toast.makeText (MainActivity.this,"差",Toast.LENGTH_LONG).show ();
            }
        });
                dialog2.show ();
    }

    private void showListDialog(){
    
    
        final String[] items = {
    
    "小朱","大朱"};
        AlertDialog.Builder dialog3 = new AlertDialog.Builder (this).setTitle ("姓名")
                .setItems (items, new DialogInterface.OnClickListener () {
    
    
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
    
    

                        Toast.makeText (MainActivity.this,"我是:"+items[which],Toast.LENGTH_LONG).show ();
                    }
                });
        dialog3.show ();
    }

    int ide = 0;
    private void showSingleDialog() {
    
    
        final String[] stars = {
    
    "苹果","香蕉","火龙果"};
        final AlertDialog.Builder dialog4 = new AlertDialog.Builder (this)
                .setTitle ("选择你喜欢的食物:")

                .setSingleChoiceItems (stars, 0, new DialogInterface.OnClickListener () {
    
    
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
    
    

                        ide = which;
                    }
                }).setPositiveButton ("确定", new DialogInterface.OnClickListener () {
    
    
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
    
    
                        Toast.makeText (MainActivity.this,"你选择了:"+stars[ide],Toast.LENGTH_LONG).show ();
                    }
                });

        dialog4.show ();
    }

    private void showMultiDialog() {
    
    
        AlertDialog.Builder dialog5 = new AlertDialog.Builder (this);
        final String[] movie = {
    
    "恐怖片","惊悚片","喜剧片","爱情片"};
        final boolean[] check = {
    
    true,false,true,false};
        dialog5.setTitle ("你想看什么电影?")

                .setMultiChoiceItems (movie, check, new DialogInterface.OnMultiChoiceClickListener () {
    
    
                    @Override

                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
    
    
                        Log.e("Log",movie[which]);
                    }
                }).setPositiveButton ("确定", new DialogInterface.OnClickListener () {
    
    
            @Override
            public void onClick(DialogInterface dialog, int which) {
    
    
                String msg = "你喜欢的电影是:";
                for (int n=0;n<check.length;n++){
    
    
                    if(check[n]){
    
    
                        msg = msg + movie[n] + " ";
                        Toast.makeText (MainActivity.this,msg,Toast.LENGTH_LONG).show ();
                    }
                }
            }
        });
        dialog5.show ();
    }

    private void showWaitDialog() {
    
    

        ProgressDialog progressDialog = new ProgressDialog (this);
        progressDialog.setTitle ("下载中....");
        progressDialog.setMessage ("请等待");
        progressDialog.show ();
    }

    private void showProgressDialog() {
    
    
        final ProgressDialog dialog7 = new ProgressDialog (this);
        dialog7.setTitle ("下载中....");
        dialog7.setMessage ("请等待");
        dialog7.setProgressStyle (ProgressDialog.STYLE_HORIZONTAL);
        dialog7.setIndeterminate (false);
        dialog7.show ();
        final Thread thread = new Thread () {
    
    
            public void run(){
    
    
                super.run ();
                for (int i = 0;i<=100;i++){
    
    
                    dialog7.setProgress (i);
                    try {
    
    
                        Thread.sleep (50);
                    } catch (InterruptedException e) {
    
    
                        e.printStackTrace ();
                    }
                }
                dialog7.dismiss ();
            }
        };
        thread.start ();
    }



}

activity_main.xml

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

    <Button
        android:id="@+id/btn_1"
        android:text="单选框"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="myclick"/>

    <Button
        android:id="@+id/btn_2"
        android:text="多选框"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="myclick"/>
    <Button
        android:id="@+id/btn_3"
        android:text="评价框"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="myclick"/>

    <Button
        android:id="@+id/btn_4"
        android:text="列表框"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="myclick"/>

    <Button
        android:id="@+id/btn_5"
        android:text="等待框"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="myclick"/>

    <Button
        android:id="@+id/btn_6"
        android:text="下载框"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="myclick"/>

    <Button
        android:id="@+id/btn_7"
        android:text="退出框"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="myclick"/>


</LinearLayout>

insert image description here

Guess you like

Origin blog.csdn.net/qq_45566213/article/details/124224693