如何在警报对话框中放置搜索栏? [英] How do I put a seek bar in an alert dialog?

查看:17
本文介绍了如何在警报对话框中放置搜索栏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在单击按钮后弹出一个警告对话框以显示搜索栏,以便人们可以将值从 1 更改为 48.我制作了一个自定义 xml,它有一个搜索栏和两个文本视图,我希望对话框有两个按钮,一个只是取消对话框,另一个做更多的工作.这是我到目前为止的代码.

I want an alert dialog box to pop up after I click a button to have a seek bar so that the person can change the value from 1-48. I've made a custom xml that has a seek bar and two text view and I would like the dialog box to have two buttons, one just cancels the dialog and the other does more work. Here is the code I have so far.

  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
    <SeekBar android:max="48" android:layout_height="wrap_content" android:id="@+id/seekBar1" android:layout_width="fill_parent" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_marginTop="74dp"></SeekBar>
    <TextView android:text="Change Hour Range" android:layout_height="wrap_content" android:id="@+id/textView1" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_width="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true"></TextView>
    <TextView android:text="Only most recent positions" android:layout_height="wrap_content" android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_below="@+id/textView1" android:layout_centerHorizontal="true" android:layout_marginTop="15dp"></TextView>

</RelativeLayout>

这是我目前看到的警报对话框

Here is the alert dialog that I have so far

new AlertDialog.Builder(ImTracking.this)
                    //is there something like setcontentview for alert dialogs?

                    .setPositiveButton("Cancel", null)
                    .setNegativeButton("OK",
                            new DialogInterface.OnClickListener() {
 // does some work here

推荐答案

是的 builder.setView(View v); 下面是如何使用它.

Yep builder.setView(View v); here is how you can use it.

    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.yourLayoutId, (ViewGroup) findViewById(R.id.yourLayoutRoot));
    AlertDialog.Builder builder = new AlertDialog.Builder(this)
    .setView(layout);
    AlertDialog alertDialog = builder.create();
    alertDialog.show();
    SeekBar sb = (SeekBar)layout.findViewById(R.id.yourSeekBar);
    sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
            //Do something here with new value
        }
    });

在示例代码中添加了progressListener.请注意,在调用 alertDialog.show() 之前,您无法获得对 SeekBar 的引用,如果 SeekBar 未显示 findViewById() 将返回 null.另请注意,您必须使用 layout.findViewById(); 因为 SeekBar 是layout"引用的 RelativeLayout 的子级.

Added progressListener to sample code. Do note that you cannot get a reference to your SeekBar until after you call alertDialog.show(), if the SeekBar is not being shown findViewById() will return null. Also note that you must use layout.findViewById(); because the SeekBar is a child of the RelativeLayout that 'layout' is a reference to.

这篇关于如何在警报对话框中放置搜索栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆