Android, SeekBar in dialog

后端 未结 4 1915
自闭症患者
自闭症患者 2020-12-30 11:28

I would like to use a dialog with a seekbar in my application. But I don\'t really know how to do it because I lack experience with android.

So, when you press a but

4条回答
  •  清歌不尽
    2020-12-30 11:58

    Maybe you could think of create a custom dialog; it requires more work but it usually works for me ;) Create a new layout file for your dialog (say, your_dialog.xml):

    
    
    
    
    
    
    

    Then, in your activity:

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

    Thus, you can operate on your element as follows:

    Button yourDialogButton = (Button)layout.findViewById(R.id.your_dialog_button);
    SeekBar yourDialogSeekBar = (SeekBar)layout.findViewById(R.id.your_dialog_seekbar);
    // ...
    

    and so on, to set listeners for button and seekbar.

    EDIT: The seekbar onchangelistener should be as follows:

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

提交回复
热议问题