对话框中的Android,SeekBar

Seb*_*Seb 8 android dialog seekbar

我想在我的应用程序中使用带有搜索条的对话框.但我真的不知道怎么做,因为我缺乏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();
Run Code Online (Sandbox Code Playgroud)

我该怎么做才能添加SeekBar

谢谢!

e-c*_*cal 21

也许你可以想到创建一个自定义对话框; 它需要更多的工作,但它通常适用于我;)为对话框创建一个新的布局文件(比如,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>
Run Code Online (Sandbox Code Playgroud)

然后,在您的活动中:

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);
Run Code Online (Sandbox Code Playgroud)

因此,您可以按如下方式对元素进行操作:

Button yourDialogButton = (Button)layout.findViewById(R.id.your_dialog_button);
SeekBar yourDialogSeekBar = (SeekBar)layout.findViewById(R.id.your_dialog_seekbar);
// ...
Run Code Online (Sandbox Code Playgroud)

依此类推,设置按钮和搜索栏的监听器.

编辑:seekbar 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);
Run Code Online (Sandbox Code Playgroud)


小智 6

我希望这能帮到您.试试这个代码......

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(); 
Run Code Online (Sandbox Code Playgroud)