我认为以下是一个基本问题。
我使用这个数字选择器,因为 Android SDK 没有提供: http ://www.quietlycoding.com/?p=5&cpage=1#comment-10645
我已将它集成到一个膨胀的警报对话框中。这里是膨胀的布局:
<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>
我在警报对话框中添加了一个按钮。如果单击它,我想获取数字选择器的当前值。
数字选择器类提供了一个功能:
public static int getCurrent() {
return mCurrent;
}
问题是我不知道如何调用它。我没有班级的对象。如果我通过 com.example.NumberPicker.getCurrent() 调用它,我必须将它声明为静态的,这会产生一些负面影响。
有人知道我如何获取选择器类的对象以调用 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" />
我的代码如下:
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());
--
但是现在我的应用程序在按下按钮后崩溃了..
编辑3:
解决了:我必须打电话
NumberPicker np = (NumberPicker) textEntryView.findViewById(R.id.cigarettesPicker);
np.setCurrent(1);