如何使一个"不要再&QUOT问我;对话框弹出框?安卓 [英] How to make a "do not ask me again" dialog pop-up box? Android

查看:270
本文介绍了如何使一个"不要再&QUOT问我;对话框弹出框?安卓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建,通知有关启用在手机上的GPS会降低电池的使用寿命,用户提示对话框。我希望它弹出,但有一个复选框,上面写着:不要再问我。

我如何去在Android的创造呢?

感谢您,

Zukky。

  AlertDialog.Builder提示=新AlertDialog.Builder(本);
prompt.setCancelable(假);
prompt.setTitle(警告);
prompt.setMessage(提示:否则,它会使用网络寻找+
                   您的位置。这是不准确的,但节省了+
                   电池!切换GPS上更好的准确性+
                   但请记住,它使​​用更多的电池!);
 

解决方案

修改:我,包括code在这里参考的原文作者的网站

它集机器人preferences并检查它是否将显示对话框或没有价值。

checkbox.xml 资源/布局

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / layout_root
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=横向
    机器人:填充=10dp>
    <复选框
        的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:ID =@ + ID /跳过
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=好了,请不要再显示。 >
    < /复选框>
< / LinearLayout中>
 

Activity.java

 公共类SeActivity延伸活动{
    公共静态最后弦乐preFS_NAME =我的prefsFile1;
    公开复选框dontShowAgain;

    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
    }

    / **当活动被带到前面调用。 * /
    @覆盖
    保护无效onResume(){
        AlertDialog.Builder ADB =新AlertDialog.Builder(本);
        LayoutInflater adbInflater = LayoutInflater.from(本);
        查看eulaLayout = adbInflater.inflate(R.layout.checkbox,NULL);
        dontShowAgain =(复选框)eulaLayout.findViewById(R.id.skip);
        adb.setView(eulaLayout);
        adb.setTitle(注意);
        adb.setMessage(Html.fromHtml(Zukky,我怎么能看到这个呢?));
        adb.setPositiveButton(OK,新DialogInterface.OnClickListener(){
            公共无效的onClick(DialogInterface对话,诠释它){
                字符串checkBoxResult =未选中
                如果(dontShowAgain.isChecked())
                    checkBoxResult =检查;
                共享preferences设置= getShared preferences(preFS_NAME,0);
                共享preferences.Editor编辑器= settings.edit();
                editor.putString(skipMessage,checkBoxResult);
                //提交的修改!
                editor.commit();
                startActivity(新意图(Intent.ACTION_VIEW,Uri.parse(http://misha.beshkin.lv/android-alertdialog-with-checkbox/)));
                返回;
            }
        });

        adb.setNegativeButton(取消,新DialogInterface.OnClickListener(){
            公共无效的onClick(DialogInterface对话,诠释它){
                字符串checkBoxResult =未选中
                如果(dontShowAgain.isChecked())
                    checkBoxResult =检查;
                共享preferences设置= getShared preferences(preFS_NAME,0);
                共享preferences.Editor编辑器= settings.edit();
                editor.putString(skipMessage,checkBoxResult);
                //提交的修改!
                editor.commit();
                返回;
            }
        });
        共享preferences设置= getShared preferences(preFS_NAME,0);
        字符串skipMessage = settings.getString(skipMessage,未选中);
        如果(!skipMessage.equals(选中))
            adb.show();

        super.onResume();
    }
}
 

所以,我做了复制和粘贴了。只更改了消息字符串。它精美的作品。

I am trying to create a "hint" dialog box that informs the user about enabling GPS on their phone will decrease battery life. I want it to pop-up but have a check box that says: "Do not ask me again".

How do I go about creating this in Android?

Thank you,

Zukky.

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!");

解决方案

EDIT: I'm including the code here with reference to the original author's site.

It sets a value in Android Preferences and checks it to whether it will show the dialog or not.

checkbox.xml in resources/layouts

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_root"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:padding="10dp" >
    <CheckBox
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/skip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Ok please do not show again." >
    </CheckBox>
</LinearLayout>

Activity.java

public class SeActivity extends Activity {
    public static final String PREFS_NAME = "MyPrefsFile1";
    public CheckBox dontShowAgain;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    /** Called when the activity is brought to front. */
    @Override
    protected void onResume() {
        AlertDialog.Builder adb = new AlertDialog.Builder(this);
        LayoutInflater adbInflater = LayoutInflater.from(this);
        View eulaLayout = adbInflater.inflate(R.layout.checkbox, null);
        dontShowAgain = (CheckBox) eulaLayout.findViewById(R.id.skip);
        adb.setView(eulaLayout);
        adb.setTitle("Attention");
        adb.setMessage(Html.fromHtml("Zukky, how can I see this then?"));
        adb.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                String checkBoxResult = "NOT checked";
                if (dontShowAgain.isChecked())
                    checkBoxResult = "checked";
                SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
                SharedPreferences.Editor editor = settings.edit();
                editor.putString("skipMessage", checkBoxResult);
                // Commit the edits!
                editor.commit();
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://misha.beshkin.lv/android-alertdialog-with-checkbox/")));
                return;
            }
        });

        adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                String checkBoxResult = "NOT checked";
                if (dontShowAgain.isChecked())
                    checkBoxResult = "checked";
                SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
                SharedPreferences.Editor editor = settings.edit();
                editor.putString("skipMessage", checkBoxResult);
                // Commit the edits!
                editor.commit();
                return;
            }
        });
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        String skipMessage = settings.getString("skipMessage", "NOT checked");
        if (!skipMessage.equals("checked"))
            adb.show();

        super.onResume();
    }
}

SO, I did "copy and paste" too. Changed only the message strings. It works beautifully.

这篇关于如何使一个&QUOT;不要再&QUOT问我;对话框弹出框?安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆