Android studio开发三:常见控件(单选按钮,多选按钮,Spinner,对话框等)的添加与使用

常见控件(单选按钮,多选按钮,Spinner,对话框等)的添加与使用


话不多说,直接上源代码与截图
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>

在这里插入图片描述

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zzq_Fighting

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值