使用搜索栏更改AlertDialog消息

时间:2015-12-28 19:27:43

标签: android alertdialog seekbar

我有一个警报对话框,但我想要更改消息取决于搜索栏中的值

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

2 个答案:

答案 0 :(得分:1)

  1. 删除popDialog.show();
  2. 在 seek.setOnSeekBarChangeListener

    之前添加

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

  3. 而不是吐司,请调用dialog.setMessage();

  4. 在末尾添加dialog.show()
  5. 如此纠正的代码:

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

答案 1 :(得分:0)

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

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() + "");
        }