gpt4 book ai didi

android - NumberPicker 在 DialogFragment 中不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:24:14 25 4
gpt4 key购买 nike

我正在学习 android 编程的基础知识,但在使用 NumberPicker 时遇到了一些问题。

我已经接近我想要的了,对话框打开了,小时和分钟数字选择器出现了,但是他们没有上面和下面的数字可以滚动。我也无法使用弹出的键盘编辑数字。我尝试添加一个 onValueChange 监听器,但是当我使用键盘时它没有生成任何事件。

任何人都可以提供有关如何解决此问题的建议吗?到目前为止,我注意到我尝试使用特定 Activity 或 fragment 中的小部件做很多事情,但该特定类不支持这些事情。

以下是相关代码和图片的 fragment :

我有一个带有两个按钮的类 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 View 的 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

最佳答案

您必须向选择器提供您要选择的内容的数组。

    String[] myStringArray = new String[]{"a","b","c"};
mMinutePicker.setMinValue(0);
mMinutePicker.setMaxValue(myStringArray.length - 1);
mMinutePicker.setDisplayedValues(myStringArray);

和改变

    Dialog alertDialog = new AlertDialog.Builder(getActivity()) .setTitle(title) .setView(inflater.inflate(R.layout.activity_time_picker, null)) 

    Dialog alertDialog = new AlertDialog.Builder(getActivity()).setTitle(title).setView(view)

您正在再次膨胀 View 。

关于android - NumberPicker 在 DialogFragment 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27748360/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com