本文介绍了更新搜索栏使用的EditText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个BMI计算器和我添加了一个可选的搜索栏从EditText上更改值。当我移动搜索栏从EditText上的值是不同的,但是当我从EditText上改变搜索栏不更新。

能osmeone explcain我该怎么办呢?我在与gettext和SetProgress问题

 公共类MainActivity延伸活动{
私人RadioGroup中rgsexo;
的EditText editPeso;
的EditText editAltura;
TextView的imcView;
搜索栏alterarAltura;
搜索栏alterarPeso;@覆盖
保护无效的onCreate(捆绑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 =(搜索栏)findViewById(R.id.alterarAltura);
    alterarPeso =(搜索栏)findViewById(R.id.alterarPeso);
    alterarAltura.setOnSeekBarChangeListener(alteraralturaListener);
    alterarPeso.setOnSeekBarChangeListener(alterarpesoListener);
    //这是我遇到的麻烦
    editAltura.addTextChangedListener(新TextWatcher(){
        公共无效afterTextChanged(编辑S){
            双setAltura =(editAltura.getText.toString())* .01d;
            alterarAltura.setProgress(的String.format(%02F,setAltura).replace(',',)'。');
        }
        公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数后INT){}
        公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){}
    });
}私人OnSeekBarChangeListener alteraralturaListener =新OnSeekBarChangeListener(){    @覆盖
    公共无效onProgressChanged(搜索栏搜索栏,INT进步,
    布尔FROMUSER){
        // TODO自动生成方法存根        双setAltura =(alterarAltura.getProgress())* .01d;
        // MOSTRA纳联邦储蓄银行Ø新生的勇气
        editAltura.setText(的String.format(%02F,setAltura).replace(',',)'。');
    }    @覆盖
    公共无效onStopTrackingTouch(搜索栏搜索栏){    }    @覆盖
    公共无效onStartTrackingTouch(搜索栏搜索栏){    }
};


解决方案

您已经使用双向功能会影响这两个功能我执行的这个节目,我已经在这个方案改变了小的修改...

您刚才输入的文本会自动改变搜索栏.......

你必须设置搜索栏的最大值那么只有你可以改变搜索栏的进展......一路走好......

 公共类MainActivity延伸活动{
私人的EditText mTextEditer;
私人搜索栏mSeekBar;
公众持股量mProgressText;
公众诠释mProgressValue;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    mTextEditer =(EditText上)findViewById(R.id.textEditer);
    mSeekBar =(搜索栏)findViewById(R.id.seekBar);
    mSeekBar.setMax(100);
    mTextEditer.addTextChangedListener(新观察者());
    mSeekBar.setOnSeekBarChangeListener(新OnSeekBarChangeListener(){
        @覆盖
        公共无效onStopTrackingTouch(搜索栏搜索栏){
        }        @覆盖
        公共无效onStartTrackingTouch(搜索栏搜索栏){
        }        @覆盖
        公共无效onProgressChanged(搜索栏搜索栏,INT进步,
                布尔FROMUSER){
        }
    });
}@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
    //充气菜单;如果是present这增加了项目操作栏。
    。getMenuInflater()膨胀(R.menu.main,菜单);
    返回true;
}公共类观察家实现TextWatcher {    @覆盖
    公共无效afterTextChanged(编辑S){
    }    @覆盖
    公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,
            INT后){    }    @覆盖
    公共无效之前onTextChanged(CharSequence中,诠释开始,诠释,
            诠释计数){
        尝试{
            如果(S!= NULL){
                mProgressText = Float.parseFloat(s.toString());
                mProgressValue =(INT)mProgressText;
                mSeekBar.setProgress(mProgressValue);
            }
        }赶上(例外的例外){
            mSeekBar.setProgress(0);
            exception.printStackTrace();
        }
    }}}

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

    }
};
解决方案

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

}

}

这篇关于更新搜索栏使用的EditText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 19:10