Android,对话框中的SeekBar

发布于 2024-11-16 22:29:52 字数 780 浏览 5 评论 0原文

我想在我的应用程序中使用带有搜索栏的对话框。 但我真的不知道该怎么做,因为我缺乏 Android 经验。

因此,当您按下按钮时:应该出现一个带有搜索栏的对话框,用户可以输入一个值,然后按“确定”按钮。

我现在的代码是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

谢谢!

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.

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.

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

How should I do to add a SeekBar?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

似梦非梦 2024-11-23 22:29:52

也许您可以考虑创建一个自定义对话框;它需要更多的工作,但通常对我有用;)
为您的对话框创建一个新的布局文件(例如 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);
// ...

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

编辑:
searchbar 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);

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.

EDIT:
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);
此刻的回忆 2024-11-23 22:29:52

我希望这会对您有所帮助。
试试这个代码...

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

I hope this will help you.
Try this Code...

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(); 
输什么也不输骨气 2024-11-23 22:29:52

这是关于如何将搜索栏放入alertdialog中的代码:
检查此链接

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

快乐编码!

This is the code on how to put seekbar in alertdialog:
check this link.

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

HAPPY CODING!

回梦 2024-11-23 22:29:52

创建一个具有 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();

Create a view having edittext. use setView() method of AlertBuilder to set the view.

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文