How do I put a seek bar in an alert dialog?

后端 未结 2 2038
名媛妹妹
名媛妹妹 2021-01-02 18:56

I want an alert dialog box to pop up after I click a button to have a seek bar so that the person can change the value from 1-48. I\'ve made a custom xml that has a seek bar

相关标签:
2条回答
  • 2021-01-02 19:48

    Yep builder.setView(View v); here is how you can use it.

        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.yourLayoutId, (ViewGroup) findViewById(R.id.yourLayoutRoot));
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
        .setView(layout);
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
        SeekBar sb = (SeekBar)layout.findViewById(R.id.yourSeekBar);
        sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
                //Do something here with new value
            }
        });
    

    Edit: Added progressListener to sample code. Do note that you cannot get a reference to your SeekBar until after you call alertDialog.show(), if the SeekBar is not being shown findViewById() will return null. Also note that you must use layout.findViewById(); because the SeekBar is a child of the RelativeLayout that 'layout' is a reference to.

    0 讨论(0)
  • 2021-01-02 19:52
        //This is the code you seek!
    
     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);
      }
    

    Or you can look on this tutorial

    0 讨论(0)
提交回复
热议问题