audriod studio 两种d窗方法

audriod studio 两种d窗方法,第1张

audriod studio 两种d窗方法

一、根据日期判断软件是否超期,超期d窗,按钮退出程序,不超期继续运行。

方法:调用系统当前时期与设定日期进行比较(compareto),如果大于0则超期,程序退出。

二、按钮d窗

xml代码




    

java全部代码:

package com.example.test;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.content.DialogInterface;
import androidx.appcompat.app.alertDialog;
import android.view.View;
import java.util.*;
import java.text.*;
import java.text.ParseException;

public class MainActivity extends AppCompatActivity {


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

    }
    //以下是根据日期判断d窗
    private void showNormalDialog() {
        
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date1= new Date();
        Date date2 = null;
        try {
            date2 = sdf.parse("2021-10-23");
            if (date1.compareTo(date2) > 0) {
                final alertDialog.Builder normalDialog =
                        new alertDialog.Builder(MainActivity.this);
                //normalDialog.setIcon(R.drawable.icon_dialog);
                normalDialog.setTitle("程序使用期已过");
                normalDialog.setMessage("你需要付出点什么才可以继续使用!");

                normalDialog.setNegativeButton("关闭",
                        new DialogInterface.onClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                finish();//...To-do
                            }
                        });
                // 显示
                normalDialog.show();

            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }


    //以下是按钮d窗
    public void onDialogClick(View v){
        new alertDialog.Builder(MainActivity.this)
                .setIcon(android.R.drawable.ic_dialog_alert)
                .setTitle("注意")
                .setMessage("确定要退出么?")
                .setPositiveButton("确定", new DialogInterface.onClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        finish();//Exit Activity
                    }
                })
                .setNegativeButton("取消", new DialogInterface.onClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                    }
                }).create().show();
    }
}

效果:

 

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/4747565.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-08
下一篇 2022-11-08

发表评论

登录后才能评论

评论列表(0条)

保存