我该如何在警告对话框中添加一个进度条?

7

我希望在点击按钮后弹出一个警示对话框,其中包含一个进度条,以便用户可以更改值从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个回答

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是'layout'引用的RelativeLayout的子级。


正如您所看到的,我在自定义对话框布局中放置了一个SeekBar。我应该把SeekBar的方法(例如onProgressChanged等)放在哪里? - Sean
您可以像平常一样设置progressListener。请参见编辑。 - FoamyGuy

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);
  }

你也可以查看这个教程


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接