Android 如何指定我在EditText上写的内容。。。。!

Android 如何指定我在EditText上写的内容。。。。!,android,android-edittext,Android,Android Edittext,我想用自定义键盘键入EditText。假设我想写数字1,有没有什么方法可以让我在写了数字1后禁用它。即使用户尝试按键盘上的数字1,它也不会写入,但如果用户使用退格删除任何数字,他可以再次使用已删除的数字。对于数字0-9,是否可以这样做 公共类BasicOnKeyboardActionListener实现OnKeyboardActionListener{ private Activity mTargetActivity; public BasicOnKeyboardActionListener

我想用自定义键盘键入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);
}
KeyboardWidgetAutoriactivity.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>

无法直接从键盘禁用它。但是,您可以始终在
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();
        }
    });

pike是正确的,使用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();
        }
    });
编辑:

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


            }
        });
String textContainer=”“;
整数长度=0;
int-prevLength=0;
editText.addTextChangedListener(新的TextWatcher(){
@凌驾
更改前文本之前的公共void(CharSequence CharSequence、int start、int count、int after){
textContainer=s.toString();
prevLength=s.length();
}
@凌驾
public void onTextChanged(CharSequence CharSequence、int start、int before、int count){
}
@凌驾
public void PostTextChanged(可编辑){
长度=s.长度();
如果(!textContainer.isEmpty()){
如果(s.长度()>1){
如果(前置长度<长度){
如果(!textContainer.contains(s.toString().子序列(长度-1,长度))){
长度=s.长度();
}否则{
PhoneNumberItemText.getText().delete(长度-1,长度);
}
}
}
}否则{
textContainer=s.toString();
}
}
});

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

创建一个自定义键盘,按下按钮后禁用该按钮。在退格键上,读取最后一个单词“启用它”


使用Fragment显示或隐藏您的键盘。您必须跳过编辑文本的焦点,才能不显示默认键盘,而显示您的键盘。

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

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


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

我希望当用户写入数字(例如1234)时,数字1234禁用,这样他就不能再次写入,直到他将其退格当我写1的时候,我不想再出现任何数字1,即使按了键,但我不知道我搜索网络两天的方式,以便从键盘上禁用它,也就是说,我想,但我不知道如何做我应该把它放入文本容器(“1,2,3,4,5,6,7,8,9,0”)?我更新了答案,您现在不应该输入任何数字。第二个if缺少一个右括号“”添加一个。首先,您将从当前文本及其以前的长度中获取一个值,更改后,您将检查新文本长度是否大于以前的长度,以便知道用户已添加了一个字符。然后,您将检查最后添加的字符是否已存在于当前文本中,如果存在,则删除它。实际上我搜索了整个网络,我没有找到显示自定义键盘上1个按钮的代码。你能帮我处理代码吗?sry im NewBee你必须设计的键盘将是另一个布局。为这些数字添加按钮,并在所有按钮上放置一个onclickListener。getWindow()。setSoftInputMode(WindowManager.LayoutParams.SOFT\u输入\u状态\u隐藏);使用此选项隐藏默认键盘。我的键盘是一个xml文件自定义键盘。我只在其中输入数字,然后退格并输入按钮是的,然后在连接活动和内容时将其与java文件连接,并指定单击侦听器。将其与java文件连接??我不明白这一点我有自定义键盘,是的,我有onKey()检查我的代码…!我只有1个edittext如果你只需要1个edittext,那么textwatcher是可以的:)只需要取值,检查它,然后执行tricki尝试的代码Vryin编写不起作用,我找不到任何其他示例,我不知道如何输入值并禁用它(如果存在):(