以编程方式控制EditText中的光标和文本

时间:2014-06-27 06:35:10

标签: java android android-edittext

我有一个EditText框,我想在其中控制光标并以编程方式修改文本

  • 我在GridView中使用12个按钮制作了12个按钮键盘。按下每个按钮,我有一个特定的文本,它将被插入光标位置的EditText框中。为此我需要光标位置,以便我可以在EditText视图中插入我的自定义文本
  • 我有两个按钮,用于将光标位置左/右移动一个字符。或者,也可以通过触摸EditText视图(如EditText应该表现的那样)设置光标
  • 每当光标位置发生变化时,我都希望光标的当前位置在EditText中(我想我必须实现某种界面,但我不知道如何)

到目前为止我尝试了什么

  • 我将按键存储在ArrayList<String>
  • 我每按一次键设置edittext.setText(String)
  • 可以通过getText()获取可修改的文字,但setText()只接受字符串。

因此我很困惑。我该怎么做才能满足我的所有要求 PS:我是Android初学者,正在制作我的第二个应用程序(如果它有帮助,这是一个科学计算器)
如果有人自愿审查我的代码,我将非常感激他

3 个答案:

答案 0 :(得分:1)

要在EditText中设置任何数字,请使用Wrapper类,然后使用toString()在EditText上设置它。

对于设定位置,您可以使用editText.setSelection(position);

答案 1 :(得分:1)

我不知道为什么你需要textview的光标位置,但在这里看看这个问题:Get Cursor Position in Android in Edit Text?

实际上,您可以通过获取输入来编辑textview上的文本,或者如果您希望可以实现TextWatcher接口,以了解用户在textview中输入的每个输入,如下所示:

 private class CheckText implements TextWatcher {
    @Override
        public void afterTextChanged(Editable s) {
                  //triggers after the user changed the input on text
        }
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
                      //triggers before the user changed the input on text
        }
        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
                   //triggers when the user changed the input on text
        }
    }

答案 2 :(得分:1)

这是我做类似事情的方式

  1. 存储数组中所有按钮的id。如下所示。

    int[] kbdButtons = { R.id.button1, R.id.button2, R.id.button3,
            R.id.button4, R.id.button5, R.id.button6, R.id.button7,
            R.id.button8, R.id.button9, R.id.button10, R.id.button11,
            R.id.button12, R.id.button13, R.id.button14, R.id.button15}
    

    2.然后将自定义Onclicklistner添加到kbdButtons数组中的所有按钮

     for (int i = 0; i < kbdButtons.length; i++) {
            Button buttonNum = (Button) dialoglayout
                    .findViewById(kbdButtons[i]);
            buttonNum.setOnClickListener(hindiKbdBtnsClick);
        }
    
  2. 这是自定义点击列表的声明

    private OnClickListener hindiKbdBtnsClick = new OnClickListener() {
        @Override
        public void onClick(View v) {
            int btnId = v.getId();
            if (isShift && btnId != R.id.kbdKeyLeftShift
                    && btnId != R.id.kbdKeyRightShift) {
                sNotPressedView.setVisibility(View.VISIBLE);
                sPressedView.setVisibility(View.GONE);
                isShift = false;
            }
            if (btnId == R.id.kbdKeyLeftShift || btnId == R.id.kbdKeyRightShift) {
                if (!isShift) {
                    sNotPressedView.setVisibility(View.GONE);
                    sPressedView.setVisibility(View.VISIBLE);
                    isShift = true;
                } else {
                    sNotPressedView.setVisibility(View.VISIBLE);
                    sPressedView.setVisibility(View.GONE);
                    isShift = false;
                }
    
            } else if (btnId == R.id.kbdKeySpace || btnId == R.id.kbdKeyEnter) {
                hkEditText.append(" ");
            } else if (btnId == R.id.kbdKeyBackSpace) {
                String txt_curr_val = hkEditText.getText().toString();
                if (txt_curr_val.length() != 0)
                    txt_curr_val = txt_curr_val.substring(0,
                            txt_curr_val.length() - 1);
                hkEditText.setText(txt_curr_val);
                hkEditText.setSelection(txt_curr_val.length());
            }else if (btnId == R.id.kbdKeyHide) {
                mDialog.hide();
            }else {
                Button b = (Button) v;
                String btnText = b.getText().toString();
                hkEditText.append(btnText);
            }
    
        }
    };
    
  3. 解释 - hkEditText是我的editText视图,即

    EditText hkEditText = (EditText)FindViewById(R.id.myEdittextId);
    

    - 请注意我刚刚添加了当时按下的按钮上的文字。 - 你还可以看到一些特殊按钮的功能,如空格,移位和输入

相关问题