app选择不再提醒,对话框将不再提示

app 在首次登陆的时候,有提示需求弹出对话框。如果点击不再提示,下次登录的时候。对话框将 不再弹出。

public class MainActivity extends ActionBarActivity {

    private AlertDialog di;
    private View checkbox;
    private CheckBox cBox;

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

        //加载checkbox.xml 文件
        LayoutInflater lauoutInflater=LayoutInflater.from(this);
        checkbox = lauoutInflater.inflate(R.layout.checkbox, null);

        //获取.xml文件的按钮
         cBox=(CheckBox)checkbox.findViewById(R.id.check);
         //通过sharedPreferences来保存信息
         SharedPreferences pre=getSharedPreferences("checkvalue", MODE_PRIVATE);
         String value=pre.getString("ischeck", "");
         //判断接受到的信息

         if (value.endsWith("1")) {
             //如果选择的是1,则对话框就不再弹出
            createDialog().dismiss();
        }else {
            //如果没有选择,则对话框继续弹出
            createDialog().show();
        }
    }
@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

     private AlertDialog createDialog(){
         di=new AlertDialog.Builder(this).setTitle("友情提示ʾ").setMessage("为了保证随时接收到呼叫,需要root权限。如有需要请到设置中进行修改。").setView(checkbox)
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick( DialogInterface dialog, int which) {
                            //将数据保存到sharedPerferences中:
                            SharedPreferences pre=getSharedPreferences("checkvalue", MODE_PRIVATE);
                            Editor editor=pre.edit();
                            //判断cBox是否被选中
                            if (cBox.isChecked()) {
                                editor.putString("ischeck", "1");
                            }else {
                                editor.putString("ischeck", "0");
                            }
                            //提交选择的check,并且保存在pre中
                            editor.commit();    
                        }
                    }).create();
         return di;
       }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {


        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}


添加checkbox.xml文件

``
<?xml version="1.0" encoding="utf-8"?>
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/check"
    android:text="不再提醒"
    >
</CheckBox>
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

duolezengjie

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值