里面的片段自定义数字选择器 [英] Custom number picker inside fragment

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

问题描述

请帮我用 NumberPicker 通过 HTTPS ://github.com/SimonVT/android-numberpicker
我里面,我有按钮片段。点击按钮后,我想告诉我的号码选择器(如弹出,没有新画面)。
所以我做了什么:
我创建 NumberPickerCustomDialog

public class NumberPickerCustomDialog extends DialogFragment {
Context context;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // get context
    context = getActivity().getApplicationContext();
    // make dialog object
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // get the layout inflater
    LayoutInflater li = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    // inflate our custom layout for the dialog to a View
    View view = li.inflate(R.layout.activity_dark, null);
    // inform the dialog it has a custom View
    builder.setView(view);
    // and if you need to call some method of the class
    NumberPicker np = (NumberPicker) view
            .findViewById(R.id.numberPicker);

    // create the dialog from the builder then show
    return builder.create();
}

}

现在,在我的片段,点击按钮后,我试图表明我的号码选择器:

And now in my fragment, after clicking the button I try to show my number picker:

FragmentManager fm = getFragmentManager();
            NumberPickerCustomDialog yourDialog = new NumberPickerCustomDialog();
            yourDialog.show(fm, "some_optional_tag");

我的布局看起来:

My layout looks:

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

<net.simonvt.numberpicker.NumberPicker
    android:id="@+id/numberPicker"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

我包括进库项目,复制布局和遇到错误:

I included library into project, copied layouts and got error:

04-17 11:33:28.264: E/AndroidRuntime(10891): FATAL EXCEPTION: main
04-17 11:33:28.264: E/AndroidRuntime(10891): android.view.InflateException: Binary XML file line #8: Error inflating class net.simonvt.numberpicker.NumberPicker
04-17 11:33:28.264: E/AndroidRuntime(10891):    at android.view.LayoutInflater.createView(LayoutInflater.java:613)
04-17 11:33:28.264: E/AndroidRuntime(10891):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
04-17 11:33:28.264: E/AndroidRuntime(10891):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
04-17 11:33:28.264: E/AndroidRuntime(10891):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
04-17 11:33:28.264: E/AndroidRuntime(10891):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
04-17 11:33:28.264: E/AndroidRuntime(10891):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
04-17 11:33:28.264: E/AndroidRuntime(10891):    at pl.package.jp.Me.NumberPickerCustomDialog.onCreateDialog(NumberPickerCustomDialog.java:26)
04-17 11:33:28.264: E/AndroidRuntime(10891):    at android.support.v4.app.DialogFragment.getLayoutInflater(DialogFragment.java:295)
04-17 11:33:28.264: E/AndroidRuntime(10891):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
04-17 11:33:28.264: E/AndroidRuntime(10891):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
04-17 11:33:28.264: E/AndroidRuntime(10891):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
04-17 11:33:28.264: E/AndroidRuntime(10891):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
04-17 11:33:28.264: E/AndroidRuntime(10891):    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)

我在做什么错了?

What am I doing wrong?

推荐答案

好吧,我解决了这个问题。该库需要定义样式,即:

Ok, I solved the problem. This library requires defining styles, i.e.:

<style name="SampleTheme" parent="android:Theme">
    <item name="numberPickerStyle">@style/NPWidget.Holo.NumberPicker</item>
</style>

<style name="SampleTheme.Light" parent="android:Theme.Light">
    <item name="numberPickerStyle">@style/NPWidget.Holo.Light.NumberPicker</item>
</style>

和在AndroidManifest.xml中,风格必须为活动的,要使用你的自定义数字选择器来定义:

and in AndroidManifest.xml, style must be defined for activity, where you want to use your custom number picker:

<activity android:name=".Activity"
        android:theme="@style/SampleTheme.Light">

</activity>

这篇关于里面的片段自定义数字选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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