通过使用2个NumberPickers创建对话框需要帮助

时间:2012-12-14 16:34:22

标签: android numberpicker

我正在尝试使用2个NumberPickers创建一个对话框。我希望它们是默认的Holo主题风格。我找不到任何好的例子。到目前为止,我得到了:

    LayoutInflater inflater = (LayoutInflater)
        getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View npView = inflater.inflate(R.layout.number_picker_dialog, null);
        NumberPicker minPicker = (NumberPicker) npView.findViewById(R.id.min_picker);
        minPicker.setMaxValue(100);
        minPicker.setMinValue(0);
        NumberPicker maxPicker = (NumberPicker) npView.findViewById(R.id.max_picker);
        maxPicker.setMaxValue(100);
        maxPicker.setMinValue(0);

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Text Size:");
        builder.setView(npView);
        builder.setPositiveButton("Okay",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                }
            });
       builder.setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                    }
                });
       dialog = builder.create();
       dialog.show();

number_picker_dialog xml:

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:holo="http://schemas.android.com/apk/res-auto"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="horizontal" 
 android:gravity="center">

<NumberPicker 
    android:id="@+id/min_picker"
    android:width="100dip"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:/>

<NumberPicker 
    android:id="@+id/max_picker"
    android:width="100dip"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"/>

</LinearLayout>

但是colorpicker文本是白色的(就像背景一样),我不能设置NumberPicker的textColor。

如何设置textColor或者有人知道一个好的NumberPicker示例吗?

3 个答案:

答案 0 :(得分:1)

我发现问题为什么我的NumberPicker不是Holo.Light样式:

而不是打电话:

   LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   View npView = inflater.inflate(R.layout.number_picker_dialog, null);

我通过致电解决了这个问题:

 View npView = getLayoutInflater().inflate(R.layout.number_picker_dialog, null);

答案 1 :(得分:0)

我的方法是创建一个Custom Dialog类,如下所示。

public class CustomDialog extends Dialog {
     public CustomDialog(Context context) {
        super(context, R.style.customDialog); //use your style id from styles.xml
     }

     public void setNumberDialog() {
          setContentView(R.layout.number_picker_dialog);
          //add required listeners
          show();
     }
}

从调用acitivty调用对话框。

 new CustomDialog(context).setNumberDialog(); 

样式参数在styles.xml中定义

<style name="customDialog" parent="android:Theme.Dialog">
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
    <item name="android:background">@android:color/transparent</item>
    <item name="android:textColor">@color/textColorWhite</item>
</style>

答案 2 :(得分:0)

尝试:

<NumberPicker
    style="@android:style/TextAppearance">
</NumberPicker>