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

时间:2011-08-25 01:17:20

标签: android dialog alert 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>

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

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

2 个答案:

答案 0 :(得分: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
        }
    });

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

答案 1 :(得分:2)

    //This is the code you seek!

 public void ShowDialog(){
   final AlertDialog.Builder popDialog = new AlertDialog.Builder(this);
 final SeekBar seek = new SeekBar(this);
   seek.setMax(255);
  seek.setKeyProgressIncrement(1);

 popDialog.setIcon(android.R.drawable.btn_star_big_on);
popDialog.setTitle("Please Select Into Your Desired Brightness ");
popDialog.setView(seek);


 seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {



 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){


  txtView.setText("Value of : " + progress);
  }

或者您可以查看此tutorial