Update SeekBar with EditText

半世苍凉 提交于 2019-12-13 04:01:58

问题


i'm developing an BMI calculator and i added an optional SeekBar to change the values from the edittext. When i move the seekbar the values from the edittext are different, but the seekbar doesn't update when i change the from the edittext.

Could osmeone explcain how can i do that? I'm having problems with getText and SetProgress

public class MainActivity extends Activity {
private RadioGroup rgsexo;
EditText editPeso;
EditText editAltura;
TextView imcView;
SeekBar alterarAltura;
SeekBar alterarPeso;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    editPeso = (EditText)findViewById(R.id.editPeso);
    editAltura = (EditText)findViewById(R.id.editAltura);
    imcView = (TextView)findViewById(R.id.imcView);
    alterarAltura = (SeekBar)findViewById(R.id.alterarAltura);
    alterarPeso = (SeekBar)findViewById(R.id.alterarPeso);
    alterarAltura.setOnSeekBarChangeListener(alteraralturaListener);
    alterarPeso.setOnSeekBarChangeListener(alterarpesoListener);
    //This is where i'm having troubles
    editAltura.addTextChangedListener(new TextWatcher(){
        public void afterTextChanged(Editable s) {
            double setAltura = (editAltura.getText.toString()) * .01d;
            alterarAltura.setProgress(String.format("%.02f", setAltura).replace(',', '.'));
        }
        public void beforeTextChanged(CharSequence s, int start, int count, int after){}
        public void onTextChanged(CharSequence s, int start, int before, int count){}
    }); 
}

private OnSeekBarChangeListener alteraralturaListener = new OnSeekBarChangeListener() {

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
    boolean fromUser) {
        // TODO Auto-generated method stub

        double setAltura = (alterarAltura.getProgress()) * .01d;
        // mostra na caixa o valor novo
        editAltura.setText(String.format("%.02f", setAltura).replace(',', '.'));
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }
};

回答1:


You have Used Two way Function it will affect the Both function i executed the this program I have changed the small modification in this program...

You just Enter the Text it will change the Seek bar automatically.......

And you have to set the Maximum value of the Seek bar then only you can change the Seek bar Progress...all the best...

public class MainActivity extends Activity {
private EditText mTextEditer;
private SeekBar mSeekBar;
public float mProgressText;
public int mProgressValue;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mTextEditer = (EditText) findViewById(R.id.textEditer);
    mSeekBar = (SeekBar) findViewById(R.id.seekBar);
    mSeekBar.setMax(100);
    mTextEditer.addTextChangedListener(new watcher());
    mSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public class watcher implements TextWatcher {

    @Override
    public void afterTextChanged(Editable s) {
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
        try {
            if (s != null) {
                mProgressText = Float.parseFloat(s.toString());
                mProgressValue = (int) mProgressText;
                mSeekBar.setProgress(mProgressValue);
            }
        } catch (Exception exception) {
            mSeekBar.setProgress(0);
            exception.printStackTrace();
        }
    }

}

}


来源:https://stackoverflow.com/questions/22185421/update-seekbar-with-edittext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!