【问题标题】:Need help by creating a dialog with 2 NumberPickers通过使用 2 个 NumberPicker 创建一个对话框需要帮助
【发布时间】:2012-12-02 16:36:54
【问题描述】:

我正在尝试创建一个包含 2 个 NumberPicker 的对话框。我希望它们是默认的 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>

但是颜色选择器文本是白色的(像背景一样),我无法设置 NumberPicker 的 textColor。

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

【问题讨论】:

  • 对 NumberPicker 应用一个样式,该样式的 textViewStyle 设置为您想要的文本颜色。

标签: android numberpicker


【解决方案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);

【讨论】:

  • 不确定为什么此更改对您有用。 beforeafter 语句是等效的。你可以测试它 - getLayoutInflater() == (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE) ==> true
【解决方案2】:

我的方法是创建一个自定义对话框类,如下所示。

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

通过调用活动来调用对话框。

 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>

【讨论】:

  • 谢谢你的帮助,但我发现了真正的问题
【解决方案3】:

试试:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-19
    • 2013-07-05
    • 2012-04-10
    • 1970-01-01
    相关资源
    最近更新 更多