使用搜索栏更改 AlertDialog 消息

Change AlertDialog Message with a seek bar

我有一个警报对话框,但我想根据搜索栏中的值更改消息

Here's a screenShot

Toast 做得很好,但我不知道如何在警告对话框消息上实现 到目前为止,这是我的代码

public void requestCheckButton(View view){

    final AlertDialog.Builder popDialog = new AlertDialog.Builder(this);
    final SeekBar seek = new SeekBar(this);
    seek.setMax(11); // Para tener incrementos de 5 min

    popDialog.setTitle("¿En cuánto tiempo puedes llegar?");
    popDialog.setMessage("15 min");
    popDialog.setView(seek);
    popDialog.setPositiveButton("Mandar", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(getApplicationContext(), "Presionaste Mandar", Toast.LENGTH_SHORT).show();
        }
    });

    seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        int progress = 0;

        public void onProgressChanged(SeekBar seekBar, int progressV, boolean fromUser) {
            progress = progressV;
        }


        public void onStartTrackingTouch(SeekBar arg0) {

        }


        public void onStopTrackingTouch(SeekBar seekBar) {
            //Por cada incremento se suman 5 min
            Toast.makeText(getApplicationContext(), (progress)*5 +"min", Toast.LENGTH_SHORT).show();
        }
    });
    popDialog.show();
}

您可以使用同时包含文本视图和搜索栏的视图:

dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

 <TextView
        android:id="@+id/text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <SeekBar
        android:id="@+id/seekbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

而不是 setView(seek)

View view = View.inflate(context, R.layout.dialog, null);
final SeekBar seekBar = (Seekbar) view.findViewById(R.id.seekbar);
final TextView textView = (TextView) view.findViewById(R.id.text_view);
popDialog.setView(view);

在你的听众中:

public void onStopTrackingTouch(SeekBar seekBar) {
            textView.setText(seekBar.getProgress() + "");
        }
  1. 删除 popDialog.show();
  2. 在之前添加seek.setOnSeekBarChangeListener

    最终的 AlertDialog 对话框 = popDialog.create();

  3. 调用 dialog.setMessage();

  4. 而不是吐司
  5. 在末尾添加dialog.show()

因此更正代码:

    final AlertDialog.Builder popDialog = new AlertDialog.Builder(this);
    final SeekBar seek = new SeekBar(this);
    seek.setMax(11); // Para tener incrementos de 5 min

    popDialog.setTitle("¿En cuánto tiempo puedes llegar?");
    popDialog.setMessage("15 min");
    popDialog.setView(seek);
    popDialog.setPositiveButton("Mandar", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(getApplicationContext(), "Presionaste Mandar", Toast.LENGTH_SHORT).show();
        }
    });
    final AlertDialog dialog = popDialog.create();
    seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        int progress = 0;

        public void onProgressChanged(SeekBar seekBar, int progressV, boolean fromUser) {
            progress = progressV;
        }


        public void onStartTrackingTouch(SeekBar arg0) {

        }


        public void onStopTrackingTouch(SeekBar seekBar) {
            //Por cada incremento se suman 5 min
            dialog.setMessage((progress)*5 +"min");
        }
    });
    dialog.show();
}