- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在学习 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/
我有一个 AppCompatActivity,它在某个时候显示一个 DialogFragment。在此对话框中,有些项目在删除之前需要确认。通过另一个 Yes/No DialogFragment 询问
我们添加一个通用的/普通的 fragment ,以编程的方式做一些事情,比如: fragmentTransaction.add(containerViewId, fragmentToAdd, frag
我有一个 DialogFragment 向用户显示选项列表,其中一个选项是“删除”选项,当用户按下删除选项时我想显示另一个 DialogFragment 作为确认,不幸的是,确认对话框没有显示。 这是
假设我在一个名为 my_dialog_fragment.xml 的 xml 布局文件中指定了 DialogFragment 的布局,并指定了 layout_width 和 >layout_height
当我设置对话框 fragment 的样式时,android 允许截取对话框 fragment 的屏幕截图,但在删除样式后它会起作用。 下面是对话框 fragment 的onCreate代码 @Over
我的应用程序中有一个带有日期和时间字段的 DialogFragment1(由 MyFragmentActivity 调用)。当用户单击它们时,会出现 DatePickerFragment 和 Time
显示 ProgressDialog 时应用程序崩溃。我正在使用滑出式键盘在手机上进行测试。显示对话框时,我滑动键盘,应用崩溃。 public static class ProgressDialogFr
正如问题中提到的.. 我有一个 fragment ,我从中创建并显示一个 DialogFragment,并将 targetFragment 设置为此当前 fragment 。 DialogFragme
如何在此 DialogFragment 中检索 editText1?它存在于 vraag_banen.xml 中,但 getView() 为空。 该对话框显示正常,也没有编译错误,但是我无法弄清楚如何
我应该如何创造这个设计的优越部分? 我正在使用 DialogFragment 在弹出窗口中执行此操作,但无法实现顶部透明的效果。 按钮设计但 background_circle: 使用
最后,我发现问题是什么...... 如果你有这样的功能: public void test() { DialogFragment dlg = new LoginDialog(); dl
我有一个带有监听器的 DialogFragment,用于单击按钮以调用 fragment 中的函数。 我收到 lateinit property listener has not been initi
我在 Activity 上有一个 DialogFragment。此 DialogFragment 是一个由 Web 服务调用填充的 ListView 。单击此 ListView 中的一行后,对话框应消
我正在尝试遵循本教程:http://developer.android.com/guide/topics/ui/controls/pickers.html#DatePicker 这是他们提供的代码,所
有什么方法可以更改 DialogFragment 中正面、负面和中性按钮的显示顺序,以便我可以先放置负面按钮? 我不想改变按钮的“性质”,而是保持它们为“积极”、“消极”和“中性”。 最佳答案 尝试这
这里可能重复,抱歉... 我不知道如何将自定义样式添加到我的 DialogFragment。此刻我正在上课 public final class SelectFragment extends Dial
我花了几个小时来让这个简单的用例正常工作:当我按下音量硬件按钮时,我想打开一个 DialogFragment。该对话框包含一个 SeekBar,应根据按下的按钮进行设置。 我这里有几个问题。 首先,我
DialogFramgment 有问题(在多个 Android 4.2/4.4 设备上支持 v4 库)。 我有两个 DialogFragment:EditAccountDialogFragment 和
当我将有效代码从 fragment 复制到dialogfragment时,它突然不起作用。 在常规 fragment 上:将位图保存在 sd 卡上。 on dialogfragment: 不将位图保存
我花了一整天的时间试图弥补这一点,但我不能.. 这就是问题所在:我想要一个是/否 AlertDialog方向改变时不会消失,所以我决定使用 DialogFragment . 所以我准备了代码并第一次使
我是一名优秀的程序员,十分优秀!