使用EditText更新SeekBar

时间:2014-03-04 23:30:30

标签: java android android-edittext seekbar

我正在开发一个BMI计算器,我添加了一个可选的SeekBar来更改edittext中的值。当我移动搜索栏时,edittext中的值不同,但是当我从编辑文本中更改时,搜索栏不会更新。

osmeone可以说明我该怎么做?我遇到了getText和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 个答案:

答案 0 :(得分:2)

你已经使用了双向函数它会影响两个函数我执行了这个程序我已经改变了这个程序中的小修改...

您只需输入文本即可自动更改搜索栏.......

您必须设置搜索栏的最大值,然后才能更改搜索栏进度...一切顺利......

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

}

}