Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android限制编辑文本长度_Android_Input_Android Edittext - Fatal编程技术网

Android限制编辑文本长度

Android限制编辑文本长度,android,input,android-edittext,Android,Input,Android Edittext,我在android中限制edittext长度时遇到问题。除删除邮件外,所有操作都非常完美。 我的编辑文本控件的最大长度为16个字符,它将限制它识别、显示和读取前16个字符,但当我继续键入时,它会在后台的某个位置存储文本,如果我想删除文本,它不会开始从第16个字符向后删除,而是从我输入的最后一个字符向后删除。 请参阅下面的代码 我甚至尝试添加TextChangedListener,它不会触发输入16个字符,因为没有任何操作 我正在使用Nexus7测试此功能 布局 <?xml version=

我在android中限制edittext长度时遇到问题。除删除邮件外,所有操作都非常完美。 我的编辑文本控件的最大长度为16个字符,它将限制它识别、显示和读取前16个字符,但当我继续键入时,它会在后台的某个位置存储文本,如果我想删除文本,它不会开始从第16个字符向后删除,而是从我输入的最后一个字符向后删除。 请参阅下面的代码

我甚至尝试添加TextChangedListener,它不会触发输入16个字符,因为没有任何操作

我正在使用Nexus7测试此功能

布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:background="@drawable/background"
    android:orientation="vertical"
    android:padding="10dp" >


    <TextView
        android:id="@+id/send_new_message_lblTo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="@string/send_new_message_to"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/send_new_message_lblToValue"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@+id/send_new_message_lblTo"
        android:ems="10"
        android:textAppearance="?android:attr/textAppearanceLarge" >
    </TextView>

    <EditText
        android:id="@+id/send_new_message_txtMessage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/send_new_message_lblToValue"
        android:layout_marginTop="10dp"
        android:ems="10"
        android:hint="@string/send_new_message_hint"
        android:inputType="textMultiLine"
        android:lines="2"
        android:maxLength="16"
        android:scrollbarStyle="outsideOverlay" />

    <Button
        android:id="@+id/send_new_message_btnSend"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/send_new_message_txtMessage"
        android:layout_marginTop="10dp"
        android:text="@string/send" />

</RelativeLayout>

我无法重现你的行为。看起来一切正常,文本被正确地限制为16个字符


键盘一直追加文本的唯一位置是在其上方的字段中,但即使选择了建议的单词,在插入文本字段之前,它也会被截断。这就是为什么你看不到事件被触发的原因,因为它是IME中的一个内部事件,而不是
EditText

中的Android事件。试试这个:为什么你只为16个字符指定一个2行
EditText
?@Raffaele 2行EditText是在一段时间之前指定的,并且一直是这样,尽管如此,我已经将其设置为1行,但它仍然无法按以下方式工作:键入超过16个字符的word,然后开始使用退格删除,id不会删除它从上次输入的字符中开始删除的第16个字符。(如果你或其他任何人理解我刚才写的:)我刚刚在一些较旧的三星安卓2.3版本上尝试过这个,它的工作原理与预期一样。也许它只是在新版本的安卓系统上才表现出这一点,因为我正在更新到最新版本的Nexus7上测试它version@Damir我所能看到的是虚拟键盘不断更新其建议,甚至超过了第16个字符,因此退格符对建议起作用,结果文本显然没有反映在
TextView
中。但之所以会出现这种情况,是因为虚拟键盘具有建议功能,它与应用程序端无关,它都是IME内部的,非常感谢您的解释,我希望客户会接受:)
  import android.os.Bundle;
    import android.text.Editable;
    import android.text.TextWatcher;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    import android.widget.Toast;

    public class NewMessageSendActivity{

    private TextView to;
    private EditText message;
    private Button send;

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

    }

    private void getViewInstances() {
        to = (TextView) findViewById(R.id.send_new_message_lblToValue);
        message = (EditText) findViewById(R.id.send_new_message_txtMessage);
        send = (Button) findViewById(R.id.send_new_message_btnSend);

        to.setText(AppSettings.getInstance().getSelectedDevice().toString());
    }

    private void setControlActions() {

        send.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                String smsMessage = message.getText().toString();
                Toast.makeText(this, smsMessage, Toast.LENGTH_SHORT).show();
            }
        });
    }
}