代码之家  ›  专栏  ›  技术社区  ›  Alxlly

使用搜索栏更改AlertDialog消息

  •  0
  • Alxlly  · 技术社区  · 10 年前

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

    Here's a screenShot

    吐司做得很好,但我不知道如何在警报对话框消息上实现 这是我目前的代码

    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 回复  |  直到 10 年前
        1
  •  1
  •   Rediska    10 年前
    1. 删除popDialog.show();
    2. 添加 之前 seek.setOnSeekBarChangeListener

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

    3. 调用dialog.setMessage()代替toast;

    4. 添加对话框。最后显示()

    如此更正的代码:

        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();
    }
    
        2
  •  0
  •   Josbel Luna    10 年前

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

    对话框.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() + "");
            }