安卓学习7

1.设置进度对话框

package com.easybooks.learnwmz;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.health.PackageHealthStats;
import android.view.View;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.Random;

/*
进度对话框 最多只能设置3个按钮
   进度对话框通过ProgressDialog类实现 该类是AlertDialog的子类 但并不需使用AlertDiaglog.Builder类的create方法
   来返回对象实例 只需要new 关键子创建ProgressDialog对象即可
   进度对话框除了可以设置普通对话框需要的信息外,还需要设置两个必要的信息:进度的最大值和当前的进度。
   **public void setMax(int max)  进度的最大值
   **public void setProgress(int value) 当前的进度  初始的进度必须使用setProgress()方法设置
   逐渐递增的进度除了可以使用setProgress方法设置外,还可以使用:public void  (int diff)
   setProgress() 设置的是进度的绝对值  incrementProgressBy()设置的是进度的增量
 */
public class Test3 extends Activity {
//全局变量
    private static final int MUX_PROGRESS=100;
    private ProgressDialog progressDialog;
    private Handler handler;
    private int progress;
    private Button button1,button2;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView (R.layout.layout_test3);
//        声明控件
        button1=findViewById (R.id.bm1);
        button2=findViewById (R.id.bm2);

//        设置点击事件
        button1.setOnClickListener (new View.OnClickListener () {
            @Override
            public void onClick (View v) {
                showProgressDialog (ProgressDialog.STYLE_HORIZONTAL);
            }
        });
        button2.setOnClickListener (new View.OnClickListener () {
            @Override
            public void onClick (View v) {
                showProgressDialog (ProgressDialog.STYLE_SPINNER);
            }
        });

    }

//    定义方法 展示进度的style 的方法
    @SuppressLint("HandlerLeak")
    private  void showProgressDialog(int style){

//        创建ProgressDialog类的对象实例
        progressDialog =new ProgressDialog(this);
//        设置图标
        progressDialog.setIcon(R.drawable.noodle);
//        设置标题
        progressDialog.setTitle("正在输入....");
//        设置对话框的内容
        progressDialog.setMessage("请稍后");
        //设置进度对话的风格
        progressDialog.setProgressStyle(style);
//        进度的最大值
        progressDialog.setMax(MUX_PROGRESS);

//        设置对话框的暂停按钮
        progressDialog.setButton("暂停", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
//                删除消息队列中的信息来停止定时器
                handler.removeMessages(1);
            }
        });
        progressDialog.setButton2("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
 //                删除消息队列中的信息来停止定时器
                handler.removeMessages(1);
//                恢复初始值
                progress=0;
                progressDialog.setProgress(0);
            }
        });

        progressDialog.show();
        /*
               消息传递的机制——Handler,来帮助我们将子线程的数据传递给主线程,其实,
               当熟悉了Handler的原理之后我们知道,Handler不仅仅能将子线程的数据传递给主线程,它能实现任意两个线程的数据传递。

               *通常我们在主线程中创建一个Handler,
               * 然后重写该Handler的handlerMessage方法,可以看到该方法传入了一个参数Message,
               * 该参数就是我们从其他线程传递过来的信息。

               我们在来看下子线程中如何传递的信息,子线程通过Handler的obtainMessage()方法获取到一个Message实例,
               * 我们来看看Message的几个属性:
               * Message.what------------------>用来标识信息的int值,通过该值主线程能判断出来自不同地方的信息来源
               * Message.arg1/Message.arg2----->Message初始定义的用来传递int类型值的两个变量
               * Message.obj------------------->用来传递任何实例化对象
               * 最后通过sendMessage将Message发送出去。

               Handler所在的线程通过handlerMessage方法就能收到具体的信息了,如何判断信息的来源呢?当然是通过what值啦。
         */
        handler = new Handler () {
            @Override
            public void handleMessage (@NonNull Message msg) {
                super.handleMessage (msg);
                if (progress > MUX_PROGRESS) {
//                    关闭对话框
                    progress = 0;
                    progressDialog.dismiss();
                } else {
                    progress++;
//                    将进度递增为1
                    progressDialog.incrementProgressBy (1);
//                    随机设置下一次递增进度 的时间间隔
//                    第一个参数表示消息代码 用来表示消息队列中的信息 第二个参数表示下一次调用handleMessage要等待的毫秒数
                    handler.sendEmptyMessageDelayed (1, 50 + new Random ().nextInt (500));
//                    sendEmptyMessageDelayed可以使handleMessage方法以一定时间间隔循环执行
                }
            }
        };
//        设置进度初始值
        progress=(progress>0)?progress:0;
        progressDialog.setProgress (progress);
//        使用sendEmptyMessage()方法只能使handleMessage方法执行一次
        handler.sendEmptyMessage(1);
    }

}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/b1"
        android:text="显示列表对话框"
        android:textSize="20sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </Button>
    <Button
        android:id="@+id/b2"
        android:text="显示单选列表对话框"
        android:textSize="20sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </Button>
    <Button
        android:id="@+id/b3"
        android:text="显示多选框对话框"
        android:textSize="20sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </Button>
</LinearLayout>

 

 

2.登录

package com.easybooks.learnwmz;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.Nullable;

public class Test4 extends Activity implements View.OnClickListener {

    private Button bum;
    private LinearLayout linearLayout;

    @Override
    protected void onCreate (@Nullable Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.layout_test4);
        bum=findViewById (R.id.bun1);
        bum.setOnClickListener (this::onClick);


    }

    @Override
    public void onClick (View v) {
        linearLayout =(LinearLayout) getLayoutInflater ().inflate (R.layout.layout_test41,null);
        new AlertDialog.Builder (this).setTitle ("登录界面").setView (linearLayout).setIcon (R.drawable.noodle).setPositiveButton ("登录", new DialogInterface.OnClickListener () {
            @Override
            public void onClick (DialogInterface dialog, int which) {
                new AlertDialog.Builder (Test4.this).setMessage ("登录成功").create ().show ();
                Toast.makeText (Test4.this,"登录成功",Toast.LENGTH_SHORT).show ();
            }
        }).setNegativeButton ("取消", new DialogInterface.OnClickListener () {
            @Override
            public void onClick (DialogInterface dialog, int which) {
                new AlertDialog.Builder (Test4.this).setMessage ("退出登录").create ().show ();
            }
        }).show ();
    }
}

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/bun1"
        android:text="显示登录对话框"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </Button>

</LinearLayout>

 

 

 

3.Toast

package com.easybooks.learnwmz;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

public class Test5 extends Activity {
/*
   创建Toast对象时要注意 在创建只显示文本的Toast对象时建议使用makeTest方法,不要使用new关键字去创建
   makeTest方法就是会使用了setTest方法后还会设置一个View对象
   如果直接new 的话 用setTest方法去写的话 就不能创建View对象 会出现运行报错的情况的

 */
    @Override
    protected void onCreate (@Nullable Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.layout_test5);
        Button button1=findViewById (R.id.bn1);
        Button button2=findViewById (R.id.bn2);
        button1.setOnClickListener (new View.OnClickListener () {
            @Override
            public void onClick (View v) {
                Toast.makeText (Test5.this,"nice",Toast.LENGTH_SHORT).show ();
            }
        });
        button2.setOnClickListener (new View.OnClickListener () {
            @Override
            public void onClick (View v) {
                View view =getLayoutInflater ().inflate (R.layout.layout_test51,null);
                TextView textView=view.findViewById (R.id.t1);
                textView.setText ("nice");
                Toast toast=new Toast (Test5.this);
                toast.setView (view);
                toast.setDuration (Toast.LENGTH_SHORT);
                toast.show ();
            }
        });
    }
}

 layout_test5

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/bn1"
        android:text="显示文本的Toast"
        android:textColor="@color/black"
        android:textSize="20sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </Button>
    <Button
        android:id="@+id/bn2"
        android:text="显示带图标的文本的Toast"
        android:textColor="@color/black"
        android:textSize="20sp"
        android:drawableLeft="@drawable/noodle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </Button>

</LinearLayout>
layout_test51
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/t1"
        android:drawableLeft="@drawable/noodle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </TextView>
</LinearLayout>

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

wangmuzi557

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

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

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

打赏作者

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

抵扣说明:

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

余额充值