Android,SeekBar在对话框中 [英] Android, SeekBar in dialog

查看:154
本文介绍了Android,SeekBar在对话框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用程序中使用一个与搜索栏的对话框。
但是我真的不知道如何做,因为我没有Android的经验。

I would like to use a dialog with a seekbar in my application. But I don't really know how to do it because I lack experience with android.

所以,当你按一个按钮:一个对话框应该出现用一个搜索栏,用户可以输入一个值,然后按OK按钮。

So, when you press a button: a dialog should come up, with a seekbar and the user is able to enter a value and then press OK-button.

目前我所拥有的代码是由developer.android提供的默认代码:

The code I have at the moment is the default code by 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

谢谢!

推荐答案

也许你可以想到创建一个自定义对话框它需要更多的工作,但通常适用于我;)
为您的对话框创建一个新的布局文件(例如,your_dialog.xml):

Maybe you could think of create a custom dialog; it requires more work but it usually works for me ;) Create a new layout file for your dialog (say, 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>

然后,在您的活动中:

Then, in your activity:

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

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

Thus, you can operate on your element as follows:

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

等等,设置按钮和搜索栏的侦听器。

and so on, to set listeners for button and seekbar.

编辑:
更改的查找器应如下所示:

The seekbar onchangelistener should be as follows:

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

这篇关于Android,SeekBar在对话框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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