我正在學習android編程的基礎知識,並且在使用NumberPicker時遇到了一些麻煩。NumberPicker不能在DialogFragment中工作
我接近我想要的,打開對話框並顯示小時和分鐘數字選取器,但它們沒有上下滾動的數字。我也不能用彈出的鍵盤編輯數字。我嘗試添加一個onValueChange偵聽器,但是當我使用鍵盤時它沒有生成任何事件。
任何人都可以提供有關如何解決這個問題的建議嗎?到目前爲止,我注意到我在某些活動或片段中嘗試使用該特定類中不支持的部件時會遇到很多問題。
下面是相關的代碼的片段和圖片:
我有一個類ClockActivity有兩個按鈕:
...
public class ClockActivity extends ActionBarActivity {
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_clock);
// Define gui objects
...
mWorkForTime = (Button)findViewById(R.id.work_for_time);
mBreakForTime = (Button)findViewById(R.id.break_for_time);
// Add listeners for the work for/break for buttons
mWorkForTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Bring up the picker fragment
DialogFragment newFragment = TimePickerFragment.newInstance(R.string.work_title);
newFragment.show(getSupportFragmentManager(), "WorkTimePicker");
}
});
mBreakForTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Bring up the picker fragment
DialogFragment newFragment = TimePickerFragment.newInstance(R.string.break_title);
newFragment.show(getSupportFragmentManager(), "BreakTimePicker");
}
});
...
的TimePickerFragment類擴展DialogFragment:
public class TimePickerFragment extends DialogFragment {
// Setup the time pickers
private NumberPicker mHourPicker;
private NumberPicker mMinutePicker;
public static TimePickerFragment newInstance(int title) {
TimePickerFragment fragment = new TimePickerFragment();
Bundle args = new Bundle();
args.putInt("title", title);
fragment.setArguments(args);
return fragment;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
super.onCreateDialog(savedInstanceState);
int title = getArguments().getInt("title");
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = (View)inflater.inflate(R.layout.activity_time_picker, null);
// Initialize the pickers
mHourPicker = (NumberPicker)view.findViewById(R.id.hours_picker);
mMinutePicker = (NumberPicker)view.findViewById(R.id.minutes_picker);
mHourPicker.setMinValue(0);
mMinutePicker.setMinValue(0);
mHourPicker.setMaxValue(24);
mMinutePicker.setMaxValue(59);
Dialog alertDialog = new AlertDialog.Builder(getActivity())
.setTitle(title)
.setView(inflater.inflate(R.layout.activity_time_picker, null))
.setNegativeButton(R.string.negate,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
((ClockActivity) getActivity()).doNegativeTimeDialogClick();
}
})
.setPositiveButton(R.string.confirm,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
((ClockActivity) getActivity()).doPositiveTimeDialogClick();
}
})
.create();
return alertDialog;
}
}
以下是DialogFragment視圖的XML,位於
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/hours"
android:textSize="24sp"
android:textStyle="bold"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/minutes"
android:textSize="24sp"
android:textStyle="bold"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<NumberPicker
android:id="@+id/hours_picker"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"/>
<NumberPicker
android:id="@+id/minutes_picker"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
這是當前操作的圖像:
http://i.imgur.com/4UiSLcn.png
以下是我在尋找有:
http://www.i-programmer.info/images/stories/Core/Android/Pickers/ten.gif
下面是一些輸出我在控制檯上看到當我嘗試用軟鍵盤編輯NumberPicker時:
01-02 16:10:46.559 18438-18438/com.example.android.worktimer W/IInputConnectionWrapper﹕ beginBatchEdit on inactive InputConnection
01-02 16:10:46.569 18438-18438/com.example.android.worktimer W/IInputConnectionWrapper﹕ endBatchEdit on inactive InputConnection
如果我想要一個數字列表,該怎麼辦?我試過這個: 'mHourPicker.setMinValue(0); mMinutePicker.setMinValue(0);' 它看起來像setDisplayedValues()方法將只接受一個字符串數組。 – Burke9077
在字符串數組中設置數字,然後從數組中檢索數字,如myStringArray [mMinutePicker.getValue()] –
或者,您可以將偵聽器添加到numberpicker並檢索所選項目的位置,並在數組中找到該位置。 –