我使用按钮创建了自己的自定义键盘。我厌倦了 Android 键盘的弹出并占据了半个屏幕。我的键盘
是 Activity 布局的一部分。我将 2 个 EditText 设置为不可聚焦,以防止 Android 键盘弹出并使 EditText 只读。当我选择键盘上的按钮时,按钮文本的值将显示在我指定为焦点的 EditText 字段中(我使用 onTouch 并设置标志/boolean 值来执行此操作)。如果标志 mIsNameSelected = true
,则名称 EditText 被视为焦点,否则电子邮件 EditText 被视为焦点。这是我的困境;
此实现的第一个副作用是,当我的文本长于分配给 EditText 字段的空间时,文本会从右侧消失。我想省略开头,就好像 EditText 正常运行一样,但我认为通过将其设置为 isFocusable(false) ,我已经消除了它执行此操作的能力。有解决方法吗?
其次,为了添加用户已选择其中一个 EditText 的视觉提示,我想更改 EditText 上下划线的颜色,而不是文本链接的颜色。由于不可聚焦属性设置,文本链接不会显示。我知道必须有一种方法可以以编程方式更改 EditText 的样式,这正是我正在寻找的。
这是一个图表:
按钮调用 XML 中的 type()
方法
以下是相关方法(在 XML 中调用 type()):
public void setActivteListener(final EditText et) {
et.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int result = event.getAction();
if (result == MotionEvent.ACTION_DOWN) {
if (et.getId() == R.id.et_customer_name) {
Log.d(TAG, "here on customer name");
mIsNameSelected = true;
updateUI();
} else if (et.getId() == R.id.et_customer_email) {
Log.d(TAG, "here on customer email");
mIsNameSelected = false;
updateUI();
}
}
return true;
}
});
}
public void type(View view) {
Log.d(TAG, "type method called");
Button btn = (Button) view;
String txt = btn.getText().toString();
char value = txt.charAt(0);
if (mIsNameSelected) {
if (mCustomerName.getText().length() > 0) {
if (txt.equalsIgnoreCase("back")) {
char[] text = mCustomerName.getText().toString().toCharArray();
char[] temp = Arrays.copyOfRange(text, 0, text.length - 1);
mCustomerName.setText(String.valueOf(temp));
}
}
if (txt.equalsIgnoreCase("space")) {
char[] text = mCustomerName.getText().toString().toCharArray();
Log.d(TAG, "the value of text now : " + String.valueOf(text));
char[] temp = Arrays.copyOf(text, text.length + 1);
Log.d(TAG, "the value of temp now: " + String.valueOf(temp));
temp[temp.length - 1] = ' ';
Log.d(TAG, "temp to string: " + String.valueOf(temp));
mCustomerName.setText(String.valueOf(temp));
}
if (!(txt.equalsIgnoreCase("back") || txt.equalsIgnoreCase("space"))) {
char[] text = mCustomerName.getText().toString().toCharArray();
char[] temp = Arrays.copyOf(text, text.length + 1);
temp[temp.length - 1] = value;
mCustomerName.setText(String.valueOf(temp));
}
} else {
Log.d(TAG, "the length of the et field is : " + mCustomerName.getText().length());
if (mCustomerEmail.getText().length() > 0) {
Log.d(TAG, "the text is longer than 0");
if (txt.equalsIgnoreCase("back")) {
Log.d(TAG, "the text = back ");
char[] text = mCustomerEmail.getText().toString().toCharArray();
char[] temp = Arrays.copyOfRange(text, 0, text.length - 1);
mCustomerEmail.setText(String.valueOf(temp));
}
}
if (txt.equalsIgnoreCase("space")) {
char[] text = mCustomerEmail.getText().toString().toCharArray();
Log.d(TAG, "the value of text now : " + String.valueOf(text));
char[] temp = Arrays.copyOf(text, text.length + 1);
Log.d(TAG, "the value of temp now: " + String.valueOf(temp));
temp[temp.length - 1] = ' ';
Log.d(TAG, "temp to string: " + String.valueOf(temp));
mCustomerEmail.setText(String.valueOf(temp));
}
if (!(txt.equalsIgnoreCase("back") || txt.equalsIgnoreCase("space"))) {
char[] text = mCustomerEmail.getText().toString().toCharArray();
char[] temp = Arrays.copyOf(text, text.length + 1);
temp[temp.length - 1] = value;
mCustomerEmail.setText(String.valueOf(temp));
}
}
}
最佳答案
设置
android:windowSoftInputMode="stateAlwaysHidden"
在 list 的 Activity xml 中。您可以让 View 成为可聚焦的,而无需弹出键盘。
https://developer.android.com/guide/topics/manifest/activity-element.html
关于java - 更改EditText的下划线/样式......而不是链接,并水平滚动文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47660735/