【问题标题】:Enter to the preferences screen using sub-menu使用子菜单进入首选项屏幕
【发布时间】:2013-07-09 05:25:52
【问题描述】:

如果我想显示通知,我使用此代码从首选项屏幕进行设置。问题是只有当我创建一个按钮进入首选项屏幕时,此代码才有效。我想使用我的子菜单“设置”按钮而不是布局内的按钮继续首选项。这是代码

       // prefer
         setPref.setOnClickListener(new Button.OnClickListener(){    
                @Override    
                public void onClick(View arg0) {     
                    // TODO Auto-generated method stub   
                    Intent intent = new Intent(

                            MainActivity    .this, 

                            settings.class);

                    startActivityForResult(intent, 0);

                }});         

            checkPref();

        }

        @SuppressLint("NewApi")
        private void checkPref(){

            SharedPreferences myPref 

            = PreferenceManager.getDefaultSharedPreferences(

                    MainActivity.this);

            boolean pref_opt1 = myPref.getBoolean("pref_opt1", false); 


            if (pref_opt1){
                NotificationManager notifi = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
                Notification notification = new Notification.Builder(getApplicationContext())
                .setContentTitle("Battery Informations")
                .setContentText("Batteria al")
                .setSmallIcon(R.drawable.icon_small_not)
                //.setLargeIcon(aBitmap)
                .setTicker("Livello")
                .build();

                notification.flags = Notification.FLAG_ONGOING_EVENT;
                Intent i = new Intent(MainActivity.this, MainActivity.class); 
                PendingIntent penInt = PendingIntent.getActivity(getApplicationContext(), 0 , i , 0);
                notifi.notify(215,notification);
                } else {
                NotificationManager notifi = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
                notifi.cancel(215);
                }
        }



        @Override

        protected void onActivityResult(int requestCode, int resultCode, Intent data) {

            // TODO Auto-generated method stub

            super.onActivityResult(requestCode, resultCode, data);

            checkPref();

    }

我该怎么做?

【问题讨论】:

    标签: android button sharedpreferences android-preferences android-notifications


    【解决方案1】:

    我想使用我的子菜单“设置”按钮而不是 我的布局中的按钮。

    您正在寻找的是Options Menu,您可以通过阅读以下链接开始了解菜单:

    【讨论】:

    • 谢谢.. 但你是指整个代码还是只是一部分?来自@Override public void onClick(View arg0)... 或其他?
    【解决方案2】:

    我希望我正确理解了这个问题:您希望在单击菜单项时开始您的活动,而不是单击按钮。如果是这样,那么您可以将代码从 OnClickListener.onClick() 移动到处理菜单点击事件的方法 as described here

    【讨论】:

    • 所以您的意思是我必须将整个代码移到onOptionsItemSelected 方法中?但是我需要移动哪一部分代码?
    • 仅启动 settings.class 活动的代码 - Button.OnClickListener.onClick() 中的代码
    猜你喜欢
    • 2013-08-01
    • 1970-01-01
    • 2012-06-11
    • 1970-01-01
    • 1970-01-01
    • 2011-07-31
    • 1970-01-01
    • 2012-02-16
    • 1970-01-01
    相关资源
    最近更新 更多