用数字选择器的Andr​​oid preferenceActivity对话框 [英] Android PreferenceActivity dialog with number picker

查看:490
本文介绍了用数字选择器的Andr​​oid preferenceActivity对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过许多定制的解决方案,并解答了这个问题。我需要的东西很简单,我有一个preference活动和所有我需要的是其中的一个选项会打开与一些选择对话框并保存结果。能否请你指导我一步与如何完成这一步呢?

 公共类SettingsActivity扩展preferenceActivity
{
    @覆盖
    保护无效的onCreate(最终捆绑savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        。getFragmentManager()的BeginTransaction()代替(android.R.id.content,新的我的preferenceFragment())提交()。
        //requestWindowFeature(Window.FEATURE_NO_TITLE);
    }

    公共静态类我的preferenceFragment扩展preferenceFragment
    {
        @覆盖
        公共无效的onCreate(最终捆绑savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            加preferencesFromResource(R.xml preFS);

        }
    }

}
 

XML:          

 <开关preference
        机器人:关键=跨界
        机器人:summaryOff =十字架是无形的
        机器人:summaryOn =十字可见
        机器人:switchTextOff =OFF
        机器人:switchTextOn =ON
        机器人:标题=十字
        安卓:设置defaultValue =真/>

    <开关preference
        机器人:关键=AUTOP
        机器人:summaryOff =应用程序将进入睡眠状态
        机器人:summaryOn =应用程序不会去睡觉
        机器人:switchTextOff =OFF
        机器人:switchTextOn =ON
        机器人:标题=永远在线
        安卓:设置defaultValue =真/>

    <开关preference
        机器人:关键=tempD
        机器人:summaryOff =温度图没有显示
        机器人:summaryOn =温度图显示
        机器人:switchTextOff =OFF
        机器人:switchTextOn =ON
        机器人:标题=Tempature显示
        安卓:设置defaultValue =真/>

    <表preference
        机器人:项=@阵列/单位
        机器人:entryValues​​ =@阵列/ LUNITS
        机器人:关键=listUnits
        机器人:总结=单位schosssing
        机器人:标题=单位的Andr​​oid:设置defaultValue =C/>

     <! - 需要添加按钮,打开对话框 - >

< / preferenceScreen>
 

号码选取器的XML:

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT>

    < NumberPicker
        机器人:ID =@ + ID / numberPicker1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentTop =真
        机器人:layout_centerHorizo​​ntal =真
        机器人:layout_marginTop =64dp/>

    <按钮
        机器人:ID =@ + ID /按钮2
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_below =@ + ID / numberPicker1
        机器人:layout_marginLeft =20dp
        机器人:layout_marginTop =98dp
        机器人:layout_toRightOf =@ + ID / numberPicker1
        机器人:文本=取消/>

    <按钮
        机器人:ID =@ + ID /按钮1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignBaseline =@ + ID /按钮2
        机器人:layout_alignBottom =@ + ID /按钮2
        机器人:layout_marginRight =16DP
        机器人:layout_toLeftOf =@ + ID / numberPicker1
        机器人:文本=设置/>

< / RelativeLayout的>
 

解决方案

子类对话preference 来建立自己的 NumberPicker preference

我已经实现的一个下面为您服务。它的工作原理,但功能不完整。例如,最大值和最小值是硬$ C $光盘常数。这些确实应该在preference xml声明属性。为了得到那个工作,你需要有新的自定义属性添加attrs.xml文件。然后,它会成为一次这个小部件进入一个库项目。我已经离开了读者的工作。

您会使用小部件的任何其他preference小部件,但你必须完全限定的名称:

 < preferenceScreen的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>

    < com.example preference.NumberPicker preference
        机器人:关键=KEY_NUMBER
        机器人:标题=给我一个数字
        安卓:设置defaultValue =55/>

< / preferenceScreen>
 

NumberPicker preference.java:

 包com.example preference。

进口android.content.Context;
进口android.content.res.TypedArray;
进口的Andr​​oid preference.Dialog preference。
进口android.util.AttributeSet;
进口android.view.Gravity;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.FrameLayout;
进口android.widget.NumberPicker;

/ **
 * A {@link机器人。preference。preference}显示一个数字选择器作为一个对话框。
 * /
公共类NumberPicker preference扩展对话框preference {

    //允许的范围
    公共静态最终诠释MAX_VALUE = 100;
    公共静态最终诠释MIN_VALUE = 0;
    //启用或禁用'循环行为
    公共静态最终布尔WRAP_SELECTOR_WHEEL = TRUE;

    私人NumberPicker选择器;
    私人int值;

    公共NumberPicker preference(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);
    }

    公共NumberPicker preference(上下文的背景下,ATTRS的AttributeSet,诠释defStyleAttr){
        超(背景下,ATTRS,defStyleAttr);
    }

    @覆盖
    受保护的视图onCreateDialogView(){
        FrameLayout.LayoutParams的LayoutParams =新FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = Gravity.CENTER;

        选择器=新NumberPicker(的getContext());
        picker.setLayoutParams(的LayoutParams);

        的FrameLayout dialogView =新的FrameLayout(的getContext());
        dialogView.addView(选择器);

        返回dialogView;
    }

    @覆盖
    保护无效onBindDialogView(查看视图){
        super.onBindDialogView(视图);
        picker.setMinValue(MIN_VALUE);
        picker.setMaxValue(MAX_VALUE);
        picker.setWrapSelectorWheel(WRAP_SELECTOR_WHEEL);
        picker.setValue(的getValue());
    }

    @覆盖
    保护无效onDialogClosed(布尔positiveResult){
        如果(positiveResult){
            picker.clearFocus();
            INT为newValue = picker.getValue();
            如果(callChangeListener(为newValue)){
                的setValue(newValue)以;
            }
        }
    }

    @覆盖
    保护对象onGetDefaultValue(TypedArray一个,INT指数){
        返回a.getInt(索引,MIN_VALUE);
    }

    @覆盖
    保护无效onSetInitialValue(布尔restorePersistedValue,对象设置defaultValue){
        的setValue(restorePersistedValue getPersistedInt(MIN_VALUE):(整数)设置defaultValue);
    }

    公共无效的setValue(int值){
        THIS.VALUE =价值;
        persistInt(THIS.VALUE);
    }

    公众诠释的getValue(){
        返回THIS.VALUE;
    }
}
 

I have seen many customized solutions and answers to this question. I need something very simple, I have a preference activity and all I need is that one of the options will open dialog with a number picker and save the results. Can you please guide me step by step with how to accomplish this?

public class SettingsActivity extends PreferenceActivity
{
    @Override
    protected void onCreate(final Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
        //requestWindowFeature(Window.FEATURE_NO_TITLE);    
    }

    public static class MyPreferenceFragment extends PreferenceFragment
    {
        @Override
        public void onCreate(final Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.prefs);

        }
    }

}

XML:

    <SwitchPreference
        android:key="cross"
        android:summaryOff="Cross is invisible"
        android:summaryOn="Cross is visible"
        android:switchTextOff="OFF"
        android:switchTextOn="ON"
        android:title="Cross" 
        android:defaultValue="true"/>

    <SwitchPreference
        android:key="autoP"
        android:summaryOff="App will go to sleep"
        android:summaryOn="App will not go to sleep"
        android:switchTextOff="OFF"
        android:switchTextOn="ON"
        android:title="Always On" 
        android:defaultValue="true"/>

    <SwitchPreference
        android:key="tempD"
        android:summaryOff="Temprature not displayed"
        android:summaryOn="Temprature displayed"
        android:switchTextOff="OFF"
        android:switchTextOn="ON"
        android:title="Tempature Display" 
        android:defaultValue="true"/>

    <ListPreference
        android:entries="@array/units"
        android:entryValues="@array/lunits"
        android:key="listUnits"
        android:summary="Units schosssing"
        android:title="Units" android:defaultValue="C"/>

     <!--Need to add button to open dialog-->       

</PreferenceScreen>

Number Picker XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <NumberPicker
        android:id="@+id/numberPicker1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="64dp" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/numberPicker1"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="98dp"
        android:layout_toRightOf="@+id/numberPicker1"
        android:text="Cancel" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button2"
        android:layout_alignBottom="@+id/button2"
        android:layout_marginRight="16dp"
        android:layout_toLeftOf="@+id/numberPicker1"
        android:text="Set" />

</RelativeLayout>

解决方案

Subclass DialogPreference to build your own NumberPickerPreference.

I have implemented one below for you. It works, but is not feature complete. For example the minimum and maximum values are hard-coded constants. These should really be attributes on the preference xml declaration. To get that to work you would need to add an attrs.xml file with new custom attributes. It would then become time to move this widget into a library project. I have left that exercise to the reader.

You would use the widget as any other preference widget, except you have to fully qualify the name:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <com.example.preference.NumberPickerPreference
        android:key="key_number"
        android:title="Give me a number"
        android:defaultValue="55" />

</PreferenceScreen>

NumberPickerPreference.java:

package com.example.preference;

import android.content.Context;
import android.content.res.TypedArray;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.NumberPicker;

/**
 * A {@link android.preference.Preference} that displays a number picker as a dialog.
 */
public class NumberPickerPreference extends DialogPreference {

    // allowed range
    public static final int MAX_VALUE = 100;
    public static final int MIN_VALUE = 0;
    // enable or disable the 'circular behavior'
    public static final boolean WRAP_SELECTOR_WHEEL = true; 

    private NumberPicker picker;
    private int value;

    public NumberPickerPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public NumberPickerPreference(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected View onCreateDialogView() {
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = Gravity.CENTER;

        picker = new NumberPicker(getContext());
        picker.setLayoutParams(layoutParams);

        FrameLayout dialogView = new FrameLayout(getContext());
        dialogView.addView(picker);

        return dialogView;
    }

    @Override
    protected void onBindDialogView(View view) {
        super.onBindDialogView(view);
        picker.setMinValue(MIN_VALUE);
        picker.setMaxValue(MAX_VALUE);
        picker.setWrapSelectorWheel(WRAP_SELECTOR_WHEEL);
        picker.setValue(getValue());
    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {
        if (positiveResult) {
            picker.clearFocus();
            int newValue = picker.getValue();
            if (callChangeListener(newValue)) {
                setValue(newValue);
            }
        }
    }

    @Override
    protected Object onGetDefaultValue(TypedArray a, int index) {
        return a.getInt(index, MIN_VALUE);
    }

    @Override
    protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) {
        setValue(restorePersistedValue ? getPersistedInt(MIN_VALUE) : (Integer) defaultValue);
    }

    public void setValue(int value) {
        this.value = value;
        persistInt(this.value);
    }

    public int getValue() {
        return this.value;
    }
}

这篇关于用数字选择器的Andr​​oid preferenceActivity对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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