如何在警报对话框中放置搜索栏?

Sea*_*ean 7 alert android dialog seekbar

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

  <?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>
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

Foa*_*Guy 13

是的builder.setView(View v);这里是你如何使用它.

    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
        }
    });
Run Code Online (Sandbox Code Playgroud)

编辑:添加progressListener到示例代码.请注意,调用alertDialog.show()之后才能获得对SeekBar的引用,如果未显示SeekBar,则findViewById()将返回null.另请注意,您必须使用,layout.findViewById();因为SeekBar是RelativeLayout的子项,'layout'是对它的引用.