Android制作游标控制Edittext数值,输入edittext数值游标跟着变动

图片说明

3个回答

注意点,只有当结束变化时才去通知另一控件,不然会形成死循环

public class MainActivity extends AppCompatActivity {

    private WheelView mWheelView;//游标控件
    private EditText mMoneyEt;//金额输入框
    private int unitValue = 100;
    private int maxValue = 100000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mWheelView = (WheelView) findViewById(R.id.wheelview);
        mMoneyEt = (EditText) findViewById(R.id.et_money);

        final List<String> items = new ArrayList<>();
        for (int i = 0; i <= maxValue / unitValue; i++) {
            items.add(String.valueOf(i * unitValue));
        }


        mWheelView.setItems(items);

        mMoneyEt.setText(String.format("%1$s", ""));

        mWheelView.setOnWheelItemSelectedListener(new WheelView.OnWheelItemSelectedListener() {
            @Override
            public void onWheelItemSelected(WheelView wheelView, int position) {
                String valueStr = wheelView.getItems().get(position);
                mMoneyEt.setText(String.format("%1$s", valueStr));
            }

            @Override
            public void onWheelItemChanged(WheelView wheelView, int position) {
            }
        });

        setPricePoint(mMoneyEt, maxValue);

    }

    public void setPricePoint(final EditText editText, final float max) {
        editText.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
                //控制金额格式
                if (s.toString().contains(".")) {
                    if (s.length() - 1 - s.toString().indexOf(".") > 2) {
                        s = s.toString().subSequence(0,
                                s.toString().indexOf(".") + 3);
                        editText.setText(s);
                        editText.setSelection(s.length());
                    }
                }
                if (s.toString().trim().substring(0).equals(".")) {
                    s = "0" + s;
                    editText.setText(s);
                    editText.setSelection(2);
                }

                String input = s.toString().trim();
                if (!TextUtils.isEmpty(input)) {
                    float value = Float.valueOf(input);
                    if (value > max) {
                        s = String.valueOf(max);
                        editText.setText(String.valueOf(max));
                    }
                }

                if (s.toString().startsWith("0")
                        && s.toString().trim().length() > 1) {
                    if (!s.toString().substring(1, 2).equals(".")) {
                        editText.setText(s.subSequence(0, 1));
                        editText.setSelection(1);
                        return;
                    }
                }
            }

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

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s.toString().trim()))
                    return;
                float value = Float.valueOf(s.toString().trim());
                int position = (int) (value / unitValue);
                if (value > max) {
                    position = (int) (max / unitValue);
                }
                mWheelView.selectIndex(position);//控制游标变化
            }

        });

    }
}

Edittext 设置setOnEditorActionListener, 在onEditorAction回掉里,计算逻辑, post消息给下面的游标控件,draw游标到指定位置

u010909640
lyd19911214 我用HorizontalPickerView做的,游标能控制edittext,但是反过来就不行,本打算监听edittext,但是不知道怎么控制这个HorizontalPickerView,现在很头疼,大神能帮我做个demo吗?
大约 3 年之前 回复

效果图
图片说明
需要demo私信

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问