我如何指定我在EditText上写的东西......!

时间:2016-10-05 10:55:30

标签: android android-edittext

我想用自定义键盘输入EditText。让我说我想写1号,有没有办法让我在写完之后能禁用1号。即使用户试图从键盘上按1号也不会写,但如果他使用退格键删除任何号码,他可以再次使用删除的号码。数字0-9 ????

是否可以这样做

公共类BasicOnKeyboardActionListener实现OnKeyboardActionListener {

private Activity mTargetActivity;


public BasicOnKeyboardActionListener(Activity targetActivity) {
    mTargetActivity = targetActivity;
}

@Override
public void swipeUp() {
    // TODO Auto-generated method stub

}

@Override
public void swipeRight() {
    // TODO Auto-generated method stub

}

@Override
public void swipeLeft() {
    // TODO Auto-generated method stub

}

@Override
public void swipeDown() {
    // TODO Auto-generated method stub

}

@Override
public void onText(CharSequence text) {
    // TODO Auto-generated method stub

}

@Override
public void onRelease(int primaryCode) {
    // TODO Auto-generated method stub

}

@Override
public void onPress(int primaryCode) {
    // TODO Auto-generated method stub

}

@Override
public void onKey(int primaryCode, int[] keyCodes) {
    long eventTime = System.currentTimeMillis();
    KeyEvent event = new KeyEvent(eventTime, eventTime,
            KeyEvent.ACTION_DOWN, primaryCode, 0, 0, 0, 0,
            KeyEvent.FLAG_SOFT_KEYBOARD | KeyEvent.FLAG_KEEP_TOUCH_MODE);

    mTargetActivity.dispatchKeyEvent(event);
}

CustomKeyboardView.class

public class CustomKeyboardView extends KeyboardView {

public CustomKeyboardView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public void showWithAnimation(Animation animation) {
    animation.setAnimationListener(new AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            setVisibility(View.VISIBLE);
        }
    });

    setAnimation(animation);
}

KeyboardWidgetTutorialActivity.class

public class KeyboardWidgetTutorialActivity extends Activity {

private CustomKeyboardView mKeyboardView;
private View mTargetView;
private Keyboard mKeyboard;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    mKeyboard = new Keyboard(this, R.xml.keyboard);
    mTargetView = (EditText) findViewById(R.id.target);
    mTargetView.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            showKeyboardWithAnimation();
            return true;
        }
    });

    mKeyboardView = (CustomKeyboardView) findViewById(R.id.keyboard_view);
    mKeyboardView.setKeyboard(mKeyboard);
    mKeyboardView
            .setOnKeyboardActionListener(new BasicOnKeyboardActionListener(
                    this));
}


private void showKeyboardWithAnimation() {
    if (mKeyboardView.getVisibility() == View.GONE) {
        Animation animation = AnimationUtils
                .loadAnimation(KeyboardWidgetTutorialActivity.this,
                        R.anim.slide_in_bottom);
        mKeyboardView.showWithAnimation(animation);
    }
    @Override
    public void onBackPressed() {
         if (mKeyboardView.isShown()) {
          mKeyboardView.setVisibility(View.GONE);
        } else {
           super.onBackPressed();
        }
     }

  }
}

这是主要的xml

  <RelativeLayout android:id="@+id/LinearLayout1"
    xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout android:layout_width="fill_parent"
    android:id="@+id/container" android:layout_alignParentTop="true"
    android:layout_height="fill_parent"     android:layout_above="@+id/keyboard_view">
    <EditText android:layout_width="fill_parent" android:id="@+id/target"
        android:layout_height="wrap_content" />
</LinearLayout>

<it.anddev.tutorial.CustomKeyboardView
    android:id="@+id/keyboard_view" android:visibility="gone"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:layout_alignParentBottom="true">   </it.anddev.tutorial.CustomKeyboardView>

  </RelativeLayout>

keyboard xml

 <Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyWidth="33%p" android:horizontalGap="0px"
android:verticalGap="0px" android:keyHeight="150dip">

<Row>
    <Key android:codes="8" android:keyLabel="1" android:keyEdgeFlags="left"      />
    <Key android:codes="9" android:keyLabel="2" />
    <Key android:codes="10" android:keyLabel="3" />
    <Key android:codes="67" android:keyIcon="@drawable/sym_keyboard_delete"
        android:iconPreview="@drawable/sym_keyboard_delete"
        android:keyEdgeFlags="right" />
</Row>

<Row>
    <Key android:codes="11" android:keyLabel="4" android:keyEdgeFlags="left" />
    <Key android:codes="12" android:keyLabel="5" />
    <Key android:codes="13" android:keyLabel="6" />
    <Key android:codes="66" android:keyEdgeFlags="right"
        android:keyIcon="@drawable/sym_keyboard_feedback_return"
        android:iconPreview="@drawable/sym_keyboard_feedback_return" />
</Row>

<Row>
    <Key android:codes="14" android:keyLabel="7" android:keyEdgeFlags="left" />
    <Key android:codes="15" android:keyLabel="8" />
    <Key android:codes="16" android:keyLabel="9"    android:keyEdgeFlags="right" />
    <Key android:codes="7" android:keyLabel="0" />

    </Row>


  </Keyboard>

4 个答案:

答案 0 :(得分:2)

pike是正确的,使用textwatcher可以帮助你,但是,要实现这一点,你可以使用一个条件来阻止应用程序显示已经在edittext中的数字。

编辑:

String textContainer = "";
int length = 0;
int prevLength = 0;
    editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {
            textContainer = s.toString();
            prevLength = s.length();
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
            }
            @Override
            public void afterTextChanged(Editable editable) {
          length = s.length();
           if(!textContainer.isEmpty()){
                if(s.length() > 1) {
                    if(prevLength < length) {
                        if (!textContainer.contains(s.toString().subSequence(length - 1, length))) {
                            length = s.length();
                        } else {
                            phoneNumberEditText.getText().delete(length - 1, length);
                        }
                    }
                }
            }else{
                textContainer = s.toString();
            }


            }
        });

没有测试过这个,但这应该给你一个想法。

答案 1 :(得分:0)

无法直接从键盘禁用它。但是,您可以随时在Textwatcher上使用edittext来检查每次更改edittext中的文字时所写的内容。

   yourEdittext.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {
            String oldText = yourEdittext.getText().toString();
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
          String newText = yourEdittext.getText().toString();
        }

        @Override
        public void afterTextChanged(Editable editable) {
          String newText = yourEdittext.getText().toString();
        }
    });

答案 2 :(得分:0)

创建自定义键盘,并在按下后禁用按钮。在Backspace上读取最后一个单词Enable it back

使用片段显示或隐藏键盘。您必须将编辑文本的OnFocus覆盖为不显示默认键盘,而是键盘。

答案 3 :(得分:0)

通过textwatcher检查everysingle edittext对于您的目的是不正确的。如果你需要一个不会在应用程序的所有输入字段中输入两次值的键盘,那么你应该实现自己的键盘,就像Shuddh所说的那样。

以下是如何构建此类键盘的教程:

https://code.tutsplus.com/tutorials/create-a-custom-keyboard-on-android--cms-22615

只是一个通知,有一个方法onKey() - 你应该检查是否已输入值并忽略它。为了检查值,有一个InputConnection对象,它允许您访问当前活动的edittext等视图。

相关问题