加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

在DialogFragment中使用java – getContentResolver()和getWindo

发布时间:2020-12-14 23:48:52 所属栏目:Java 来源:网络整理
导读:我正在尝试在对话框中插入SeekBar来改变亮度.为此,我编写了这段代码. public class Lum extends DialogFragment{ private SeekBar brightbar; private int brightness; private ContentResolver cResolver; private Window window; TextView txtPerc; boolea
我正在尝试在对话框中插入SeekBar来改变亮度.为此,我编写了这段代码.
public class Lum extends DialogFragment{


        private SeekBar brightbar;
        private int brightness;
        private ContentResolver cResolver;
        private Window window;
        TextView txtPerc;
        boolean stato;
        Context context;

        public android.app.Dialog onCreateDialog(Bundle savedInstanceState) {
            // Use the Builder class for convenient dialog construction

            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

            LayoutInflater inflater = getActivity().getLayoutInflater();
            View view = inflater.inflate(R.layout.dialog_brightness,null);
            builder.setView(view);

        brightbar = (SeekBar) view.findViewById(R.id.brightbar);
        txtPerc = (TextView) view.findViewById(R.id.txtPercentage);
        //cResolver = getContentResolver();
        //window = getWindow();
        brightbar.setMax(255);
        brightbar.setKeyProgressIncrement(1);

        brightbar = (SeekBar) view.findViewById(R.id.brightbar);
        txtPerc = (TextView) view.findViewById(R.id.txtPercentage);
        cResolver = getContentResolver();
        window = getWindow();
        brightbar.setMax(255);
        brightbar.setKeyProgressIncrement(1);

        try {
            brightness = Settings.System.getInt(context.getContentResolver(),Settings.System.SCREEN_BRIGHTNESS);
        } catch (SettingNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        float perc = (brightness /(float)255)*100;
        txtPerc.setText((int)perc +" %");

        brightbar.setProgress(brightness);
        brightbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
        {
            public void onStopTrackingTouch(SeekBar seekBar)
            {
                Settings.System.putInt(context.getContentResolver(),Settings.System.SCREEN_BRIGHTNESS,brightness);
                LayoutParams layoutpars = window.getAttributes();
                layoutpars.screenBrightness = brightness / (float)255;
                window.setAttributes(layoutpars);
            }

            public void onStartTrackingTouch(SeekBar seekBar)
            {

            }

            public void onProgressChanged(SeekBar seekBar,int progress,boolean fromUser)
            {


                    brightness = progress;

                    float perc = (brightness /(float)255)*100;
                    txtPerc.setText((int)perc +" %");
            }
        });     

            builder.setTitle("Brightness")
                   .setPositiveButton("Close",new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog,int id) {
                           dismiss();
    }
                   });

            // Create the AlertDialog object and return it
            return builder.create();
        }
    }

问题是我的IDE Eclipse标记了getContentResolver和getWindow中的错误,因为我正在使用DialogFragment而不是Activity.
如何在本课程中使用此说明?或者,如果你不能这样做,还有另一种选择吗?提前致谢

解决方法

http://developer.android.com/reference/android/content/Context.html#getContentResolver()

getContentResolver()需要一个上下文.

你有context.getContentResolver().使用getActivity().getContentResolver().

http://developer.android.com/reference/android/app/Activity.html#getWindow()

同样使用getActivity().getWindow()

getActivity()

返回此片段当前与之关联的Activity.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读