【问题标题】:how I can specify the things I write on the EditText....!我如何指定我在 EditText 上写的东西......!
【发布时间】:2016-10-05 10:55:29
【问题描述】:

我想用自定义键盘在 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>

键盘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>

【问题讨论】:

    标签: android android-edittext


    【解决方案1】:

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

    尚未对此进行测试,但这应该会给您一个想法。

    【讨论】:

    • 这就是我想要的,但我不知道该怎么做
    • 我应该放入 textcontainer ("1,2,3,4,5,6,7,8,9,0") 吗?
    • 我更新了答案,你现在不应该输入任何数字。
    • 第二个如果缺少右括号 ")" 加一个。
    • 首先你从当前文本和它以前的长度中得到一个值,在改变之后,你检查新的文本长度是否大于以前的,这样你就会知道用户添加了一个特点。之后检查最后添加的字符是否已经存在于当前文本中,如果存在则删除它。
    【解决方案2】:

    没有办法直接从键盘禁用它。但是,您可以随时在您的edittext 上使用Textwatcher 来检查您每次更改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();
            }
        });
    

    【讨论】:

    • 我想当用户写数字时,例如 1234 数字 1234 禁用所以他不能再写它,直到他退格它例如他只是为数字 4 然后当他按下数字 4 写只有一次
    • 这很简单,当我写 1 我不想再出现任何数字 1 即使按下键但我不知道我在网上搜索了 2 天以禁用它键盘
    【解决方案3】:

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

    使用 Fragment 显示或隐藏您的键盘。您将不得不覆盖编辑文本的 OnFocus 以不显示默认键盘,而是显示您的键盘。

    【讨论】:

    • 实际上我搜索了整个网络我没有找到从我的自定义键盘上显示 1 个按钮的代码你能帮我写代码吗?我是新手
    • 您必须设计的键盘将是另一种布局。为这些数字添加按钮并将 onclickListener 放在所有按钮上。 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);使用它来隐藏默认键盘。
    • 我的键盘是一个 xml 文件自定义键盘,我只输入了数字和退格键并输入按钮
    • 是的,然后在连接活动和东西并分配点击侦听器时将其与 java 文件连接。
    • 用java文件连接它??我不关注你我不明白这个
    【解决方案4】:

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

    这里是如何构建这种键盘的教程:

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

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

    【讨论】:

    • 我有自定义键盘,是的,我有 onKey() 检查我的代码...!
    • 我只有 1 个 EditText
    • 如果您只需要 1 个具有这种行为的编辑文本,那么 textwatcher 就可以了 :) 只需取值,检查并完成操作
    • 我尝试了 Vryin 编写的代码不起作用,我找不到任何其他示例,我不知道如何输入我输入的值并禁用它(如果存在):(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-11
    • 2019-05-06
    • 2018-07-06
    • 2018-07-31
    • 1970-01-01
    • 1970-01-01
    • 2011-06-24
    相关资源
    最近更新 更多