带有数字选择器的Android PreferenceActivity对话框 [英] Android PreferenceActivity dialog with number picker

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

问题描述

我已经看到许多定制的解决方案和这个问题的答案。我需要一些非常简单的东西,我有一个偏好活动,我需要的只是一个选项将打开一个选择器的对话框并保存结果。你可以一步一步地指导我如何完成这个吗?

  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 =应用程序将去睡觉
android:summaryOn =应用程序不会去
android:switchTextOff =OFF
android:switchTextOn =ON
android:title =Always On
android:defaultValue =true/> ;

< SwitchPreference
android:key =tempD
android:summaryOff =Tempature not displayed
android:summaryOn =Tempature 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 =Unitsandroid:defaultValue =C/>

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

< / 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_centerHorizo​​ntal =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 =取消/>

< 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>


解决方案

子类 DialogPreference 建立你自己的 NumberPickerPreference



我已经为您实施了一个。它工作得很好,但功能不完整。例如,最小值和最大值是硬编码常量。这些应该是偏好xml声明的属性。为了使它工作,您需要添加一个指定自定义属性的attrs.xml文件。



要完整实现支持库项目中自定义xml属性的NumberPicker偏好窗口小部件,以及演示如何使用它的演示应用程序,请参阅GitHub: https://github.com/Alobar/AndroidPreferenceTest



你将使用该小部件作为任何其他偏好小部件,除非您必须完全限定名称:



preferences.xml

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

< com.example.preference.NumberPickerPreference
android:key =key_number
android:title =给我一个数字
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;

/ **
*一个{@link android.preference.Preference},显示一个数字选择器作为对话框。
* /
public class NumberPickerPreference extends DialogPreference {

//允许范围
public static final int MAX_VALUE = 100;
public static final int MIN_VALUE = 0;
//启用或禁用'循环行为'
public static final boolean WRAP_SELECTOR_WHEEL = true;

私人NumberPicker选择器
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(查看视图){
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 ,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;
}
}


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 perfectly fine, 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 specifying your custom attributes.

For the full implementation of the NumberPicker preference widget that supports custom xml attributes in a library project and a demo app showing how to use it, see GitHub: https://github.com/Alobar/AndroidPreferenceTest

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

preferences.xml

<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;
    }
}

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

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