首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何制作“不要再问我”对话框弹出框?安卓

如何制作“不要再问我”对话框弹出框?安卓
EN

Stack Overflow用户
提问于 2012-03-04 05:31:11
回答 3查看 24.2K关注 0票数 19

我正在尝试创建一个“提示”对话框,通知用户在他们的手机上启用GPS会降低电池寿命。我希望它弹出,但有一个复选框说:“不要再问我”。

我如何在Android中创建它?

谢谢,

祖基。

代码语言:javascript
复制
AlertDialog.Builder prompt = new AlertDialog.Builder(this); 
prompt.setCancelable(false); 
prompt.setTitle("Warning"); 
prompt.setMessage ("HINT: Otherwise, it will use network to find" + 
                   "your location. It's inaccurate but saves on " + 
                   "battery! Switch GPS on for better accuracy " + 
                   "but remember it uses more battery!");
EN

回答 3

Stack Overflow用户

发布于 2014-08-29 23:24:55

我有我的less代码解决方案。它并不完美,因为无法使用描述,并且只能将信息作为对话框的标题传递。checkbox使用MultiChoiceItem。

在res/values/strings.xml s.xml中:

代码语言:javascript
复制
<string-array name="do_not_show_again_array">
    <item>Do not show again.</item>
</string-array>

然后,我的代码如下所示:

代码语言:javascript
复制
DialogInterface.OnClickListener dialogClickListener = new OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
        // Do something here
    }
};
final AlertDialog.Builder builder = new AlertDialog.Builder(activity);
AlertDialog alertDialog = builder.setTitle("Title/Description")
        .setMultiChoiceItems(R.array.do_not_show_again_array, null, new OnMultiChoiceClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                appPrefs.setLocationOnStart(!isChecked);
            }
        })
        .setPositiveButton("Ja", dialogClickListener)
        .setNegativeButton("Nein", dialogClickListener).show();
}
票数 4
EN

Stack Overflow用户

发布于 2015-07-08 11:50:23

嗨,我跟着一个tutorial,我找到了这段代码

你可以使用下面的代码:

代码语言:javascript
复制
  AlertDialog.Builder adb= new

 AlertDialog.Builder(this);
  LayoutInflater adbInflater = 

 LayoutInflater.from(this);
  View eulaLayout = adbInflater.inflate

 (R.layout.activity_main, null);
  check = (CheckBox)

 eulaLayout.findViewById(R.id.skip);
  adb.setView(eulaLayout);
  adb.setTitle("Example:");
  adb.setMessage(Html.fromHtml("Type your 

  text here: "));
  adb.setPositiveButton("Ok", new

  DialogInterface.OnClickListener() {
        public void onClick(DialogInterface 

 dialog, int which) {
            String checkBoxResult = "NOT 

 checked";
            if (check.isChecked())  

 checkBoxResult = "checked";
              SharedPreferences settings = 

 getSharedPreferences(PREFS_NAME, 0);
              SharedPreferences.Editor 

 editor = settings.edit();
              editor.putString("noshow", 

 checkBoxResult);
              // Commit the edits!

            //  sunnovalthesis();

              editor.commit();
            return;
        } });

       adb.setNegativeButton("Cancel", new

    DialogInterface.OnClickListener() {
    public void onClick(DialogInterface 

     dialog, int which) {
        String checkBoxResult = "NOT 

    checked";
        if (check.isChecked())  

       checkBoxResult = "checked";
        SharedPreferences settings = 

       getSharedPreferences(PREFS_NAME, 0);
          SharedPreferences.Editor editor = 

        settings.edit();
          editor.putString("noshow", 

    checkBoxResult);
          // Commit the edits!

        //  sunnovalthesis();

          editor.commit();
            return;
    } });
     SharedPreferences settings = 

    getSharedPreferences(PREFS_NAME, 0);
  String noshow = settings.getString

     ("noshow", "NOT checked");
      if (noshow != "checked" ) adb.show();
票数 1
EN

Stack Overflow用户

发布于 2016-06-01 02:13:22

对于这个问题,我有一个清晰而正确的方法。

代码语言:javascript
复制
package com.example.user.testing;

import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;

public class MainActivity extends AppCompatActivity {
CheckBox dontShowAgain;
public static final String PREFS_NAME = "MyPrefsFile1";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final AlertDialog.Builder adb = new             AlertDialog.Builder(MainActivity.this);
    LayoutInflater adbInflater = LayoutInflater.from(MainActivity.this);
    View eulaLayout = adbInflater.inflate(R.layout.checkbox, null);

    dontShowAgain = (CheckBox) eulaLayout.findViewById(R.id.skip);
    adb.setView(eulaLayout);
    adb.setTitle("Attention");
    adb.setMessage("Your message here");
    adb.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {


            SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
            SharedPreferences.Editor editor = settings.edit();
            editor.putBoolean("skipMessage", dontShowAgain.isChecked());
            editor.commit();
            dialog.cancel();
        }
    });

    adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    Boolean skipMessage = settings.getBoolean("skipMessage", false);
    if (skipMessage.equals(false)) {
        adb.show();
    }

}

}

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9550039

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档