gpt4 book ai didi

java - 警告框不重复,输入检查不工作

转载 作者:行者123 更新时间:2023-11-29 08:31:46 25 4
gpt4 key购买 nike

我正在尝试为个人元素制作一个小型 Android 应用程序,但我的代码现在有两个问题:

  1. 代码应该执行以下直到

isAlarm == false;

                do {
(new BTask(context)).execute();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
} while (isAlarm);

但它只执行一次。

  1. 我希望它只在输入正确密码时关闭对话框,但它每次都关闭它而不执行命令:

isAlarm = false;

我知道,代码不是很漂亮,但我还在学习中。 =)

package top.sysop.myapplication;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.ToneGenerator;
import android.os.AsyncTask;
import android.os.Vibrator;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

private static final String TAG = "";
public static Button btn1;
public static Button btn2;
public static Button btn3;
private MediaPlayer mMediaPlayer;
boolean isAlarm;
private String m_Text = "";
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.context=this;
setContentView(R.layout.activity_main);
dialogevent3();
}
public void dialogevent3() {
int i=1;
btn3 = (Button) findViewById(R.id.dial3);
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Diebstahl!");
// Set up the input
final EditText input = new EditText(MainActivity.this);
// Specify the type of input expected; this, for example, sets the input as a password, and will mask the text
input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
builder.setView(input);
// Set up the buttons
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
m_Text = input.getText().toString();
if (m_Text.equals("pass"))
{
//end
isAlarm = false;
}
else
{
//repeat
dialog.dismiss();
dialogevent3();
}
}
});
do {
(new BTask(context)).execute();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
} while (isAlarm);
// alert.show();
builder.show();
}
});
}
}

最佳答案

您没有初始化 isAlarm 的值,因此它包含默认值 false。有一些变化,比如

初始化boolean isAlarm = true

while (isAlarm) 更改为 while (!isAlarm)

希望对你有帮助

关于java - 警告框不重复,输入检查不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47453242/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com