Android studio Android Studio中的AlertDialog内部复选框

Android studio Android Studio中的AlertDialog内部复选框,android-studio,android-dialog,android-checkbox,android-timer,Android Studio,Android Dialog,Android Checkbox,Android Timer,我是Android Studio的新手。我想创建一个AlertDialog,它包含一个简单的文本视图,在复选框内的每一圈时间(例如5分钟)显示一次,因此如果单击复选框,AlertDialog每5分钟显示一次。如果未单击,则什么也不显示。请帮帮我。经过一点实验,我能够创造出类似于我认为你想要的东西。这是我创建的一个小项目,但是您可以只获取项目所需的部分代码 import android.app.AlertDialog; import android.content.DialogInterface;

我是Android Studio的新手。我想创建一个AlertDialog,它包含一个简单的文本视图,在复选框内的每一圈时间(例如5分钟)显示一次,因此如果单击复选框,AlertDialog每5分钟显示一次。如果未单击,则什么也不显示。请帮帮我。

经过一点实验,我能够创造出类似于我认为你想要的东西。这是我创建的一个小项目,但是您可以只获取项目所需的部分代码

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity
{
    TextView input;
    long startTime = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        final LinearLayout ll = new LinearLayout(this);

        setContentView(ll);

        CheckBox cb = new CheckBox(getApplicationContext());
        cb.setText("Checkbox");
        ll.addView(cb);

        cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
            {
                if (isChecked)
                {
                    startTime = System.currentTimeMillis();
                    timerHandler.postDelayed(timerRunnable, 0);
                }
                else
                {
                    timerHandler.removeCallbacks(timerRunnable);
                }
            }
        });
    }

    public void showDialog()
    {
        AlertDialog.Builder alert = new AlertDialog.Builder(this);
        alert.setTitle("Title");
        alert.setMessage("Message");

        input = new TextView (this);
        alert.setView(input);
        input.setText("Text");

        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface dialog, int whichButton)
            {
                // do stuff when Ok is clicked
            }
        });

        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface dialog, int whichButton)
            {
                // do stuff when Cancel is clicked
            }
        });
        alert.show();
    }

    Handler timerHandler = new Handler();
    Runnable timerRunnable = new Runnable()
    {
        @Override
        public void run()
        {
            showDialog();
            // Edit the second parameter to whatever time you want in milliseconds
            timerHandler.postDelayed(this, 300_000);
        }
    };

    @Override
    public void onPause()
    {
        super.onPause();
        timerHandler.removeCallbacks(timerRunnable);
    }
}

希望这会有所帮助。

您是否编写了创建复选框和AlertDialog的代码?对于复选框,是的,但对于alert dialog,我不知道如何在此处找到您如何创建AlertDialog的代码,也许我可以将计时器与之集成。好的。谢谢。明天早上我会发布一个答案,因为现在我所在的地方已经很晚了。希望我能帮到你。谢谢你的时间和好意,我要写这段代码