Android,对话框中的 SeekBar

9
我想在我的应用程序中使用带有SeekBar的对话框。 但是,由于我缺乏Android方面的经验,因此不知道如何实现。
所以,当用户按下按钮时:应该弹出一个对话框,其中包含一个SeekBar,用户可以输入一个值,然后按下确定按钮。
目前我拥有的代码是developer.android提供的默认代码。
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
        .setCancelable(false)
       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                MyActivity.this.finish();
       }
   })
   .setNegativeButton("No", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
       }
   });
AlertDialog alert = builder.create();

我该如何添加一个 SeekBar

谢谢!

4个回答

24

也许你可以考虑创建自定义对话框;这需要更多的工作,但通常对我有用 ;) 为您的对话框创建一个新的布局文件(比如,your_dialog.xml):

<RelativeLayout
android:id="@+id/your_dialog_root_element"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>

<SeekBar
    android:id="@+id/your_dialog_seekbar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
</SeekBar>

<Button
    android:id="@+id/your_dialog_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >
</Button>

然后,在你的活动中:

Dialog yourDialog = new Dialog(this);
LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.your_dialog, (ViewGroup)findViewById(R.id.your_dialog_root_element));
yourDialog.setContentView(layout);

因此,您可以按以下方式操作您的元素:

Button yourDialogButton = (Button)layout.findViewById(R.id.your_dialog_button);
SeekBar yourDialogSeekBar = (SeekBar)layout.findViewById(R.id.your_dialog_seekbar);
// ...

等等,要为按钮和进度条设置监听器。

编辑: 进度条的onchangelistener应该如下所示:

OnSeekBarChangeListener yourSeekBarListener = new OnSeekBarChangeListener() {
    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
            //add code here
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
            //add code here
    }

    @Override
    public void onProgressChanged(SeekBar seekBark, int progress, boolean fromUser) {
            //add code here
    }
 };
 yourDialogSeekBar.setOnSeekBarChangeListener(yourSeekBarListener);

谢谢你的帮助!虽然我无法设置SeekBar的OnSeekBarChangeListener,因为它给了我一个错误! - Seb
我已经在答案的末尾添加了onSeekBarChangeListener存根代码,请检查一下或者告诉我哪里不行!;) - e-cal

10

我希望这能帮到你。试一下这段代码...

final AlertDialog.Builder alert = new AlertDialog.Builder(this); 

    alert.setTitle("Alert Box"); 
    alert.setMessage("Edit Text"); 

    LinearLayout linear=new LinearLayout(this); 

    linear.setOrientation(1); 
    TextView text=new TextView(this); 
    text.setText("Hello Android"); 
    text.setPadding(10, 10, 10, 10); 

    SeekBar seek=new SeekBar(this); 

    linear.addView(seek); 
    linear.addView(text); 

    alert.setView(linear); 



    alert.setPositiveButton("Ok",new DialogInterface.OnClickListener() 
    { 
        public void onClick(DialogInterface dialog,int id)  
        { 
            Toast.makeText(getApplicationContext(), "OK Pressed",Toast.LENGTH_LONG).show(); 
            finish(); 
        } 
    }); 

    alert.setNegativeButton("Cancel",new DialogInterface.OnClickListener()  
    { 
        public void onClick(DialogInterface dialog,int id)  
        { 
            Toast.makeText(getApplicationContext(), "Cancel Pressed",Toast.LENGTH_LONG).show(); 
            finish(); 
        } 
    }); 

    alert.show(); 

4
这是在AlertDialog中放置SeekBar的代码: 查看链接
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);
 }

快乐编程!


-1
创建一个包含EditText的视图。使用AlertBuilder的setView()方法来设置视图。

mytest.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <EditText
        android:id="@+id/textview1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"

        android:textColor="#000000"
        android:hint="8+ characters"
        android:maxLines="1"
        android:maxLength="18"
        android:imeOptions="actionSend|flagNoEnterAction" />

</RelativeLayout>



    AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(entryView);
    builder.setMessage("Are you sure you want to exit?")
            .setCancelable(false)
           .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                    MyActivity.this.finish();
           }
       })
       .setNegativeButton("No", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
           }
       });
    AlertDialog alert = builder.create();

嗯...你的意思是我要为对话框创建一个XML文件来查看吗? - Seb

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