Android 如何调用数字选择器的函数

Android 如何调用数字选择器的函数,android,object,picker,Android,Object,Picker,我认为以下是一个基本问题 我使用这个数字选择器,因为Android SDK没有提供: 我已将其集成到一个对话框中。以下是充气布局: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation

我认为以下是一个基本问题

我使用这个数字选择器,因为Android SDK没有提供:

我已将其集成到一个对话框中。以下是充气布局:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
>
    <com.example.NumberPicker
        android:padding="10dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
问题是我不知道怎么称呼它。我没有这门课的目标。 如果我通过com.example.NumberPicker.getCurrent()调用它,我必须将其声明为静态,这有一些负面的副作用

有人知道我如何获取picker类的对象以调用getCurrent()函数吗

我认为简单地创建一个新对象不是正确的方法,因为我想从alerdialog中运行的对象调用函数

编辑: 正如我所料:

NumberPicker np = new NumberPicker(MainScreen.this);
Log.v("TEST", "" + np.getCurrent());
这给了我0

编辑2: 我添加了一个Android ID:

<com.example.NumberPicker
        android:id="@+id/numberPicker"
        android:padding="10dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
但是现在我的应用程序在按下按钮后崩溃了

编辑3:

解决了:我必须打电话

NumberPicker np = (NumberPicker) textEntryView.findViewById(R.id.cigarettesPicker);
            np.setCurrent(1);
有人知道我如何获取picker类的对象以调用getCurrent()函数吗

  • 在布局XML中的
    NumberPicker
    元素中添加一个
    android:id

  • AlertDialog
    充气布局时,在充气布局上调用
    findViewById()
    ,传入步骤1中提供的ID,并将结果转换为
    NumberPicker


  • 与其使用内置的NumberPicker,不如看看这个UI库:?Thx。我认为这是正确的方法。但我的应用程序现在崩溃了。我将我的新代码添加到上面的帖子中。
        LayoutInflater factory = LayoutInflater.from(this);
                final View textEntryView = factory.inflate(
                        R.layout.backfall_layout, null);
    
    
    
                new AlertDialog.Builder(MainScreen.this)
                        .setTitle(this.getString(R.string.relapse))
                        .setView(textEntryView)
                        .setPositiveButton("OK",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,
                                            int whichButton) {
                                    NumberPicker np = (NumberPicker) findViewById(R.id.numberPicker);
    
                                    Log.v("TEST", "" + np.getCurrent());
    
    --
    
    NumberPicker np = (NumberPicker) textEntryView.findViewById(R.id.cigarettesPicker);
                np.setCurrent(1);