Android, SeekBar в диалоге - PullRequest
       15

Android, SeekBar в диалоге

8 голосов
/ 21 июня 2011

Я хотел бы использовать диалог с поиском в моем приложении. Но я не знаю, как это сделать, потому что мне не хватает опыта работы с Android.

Итак, когда вы нажимаете кнопку: должно появиться диалоговое окно с панелью поиска, и пользователь может ввести значение, а затем нажать кнопку OK.

Код, который у меня есть на данный момент, является кодом по умолчанию от 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?

Спасибо!

Ответы [ 5 ]

20 голосов
/ 21 июня 2011

Может быть, вы могли бы подумать о создании собственного диалога; это требует больше работы, но это обычно работает для меня;) Создайте новый файл макета для вашего диалога (скажем, 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);
// ...

и т. Д., Чтобы установить слушателей для кнопки и панели поиска.

EDIT: Поиск переключателя должен быть следующим:

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);
6 голосов
/ 25 июля 2012

Надеюсь, это поможет вам.Попробуйте этот код ...

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(); 
1 голос
/ 20 октября 2015

Это код о том, как поместить seekbar в alerttdialog: проверьте эту ссылку .

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 голос
/ 11 апреля 2014

используйте эту ссылку, которая поможет вам

http://www.thaicreate.com/mobile/android-seekbar-alertdialog-popup.html

0 голосов
/ 21 июня 2011

Создайте вид, используя edittext.используйте setView() метод AlertBuilder для установки представления.

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