我在限制android中的edittext长度时遇到问题。除了删除消息外,一切正常。我的编辑文本控件的最大长度为 16 个字符,它会限制它识别、显示和读取前 16 个字符,但是当我继续输入时,它会在背景中的某个地方记住文本,如果我想删除文本,它不会开始从第 16 个字符向后删除,但从我输入的最后一个字符开始。请参见下面的代码。
我什至尝试添加 TextChangedListener 它不会触发输入 16 个字符,因为没有任何操作。
我正在使用 Nexus 7 测试此功能
布局
<?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>
活动
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();
}
});
}
}