[英]Change the underline/style of EditText…not the link, and scroll the text horizontally
I have created my own custom keyboard using buttons. 我使用按钮创建了自己的自定义键盘。 I was tired of Android's keyboard popping up and taking up half the screen.
我已经厌倦了Android的键盘弹出并占据了一半的屏幕。 My keyboard is part of the Activity's layout.
我的键盘是活动布局的一部分。 I have 2 EditTexts set to not-focusable to prevent the Android keyboard from popping up and make the EditTexts read-only.
我将2个EditTexts设置为不可聚焦,以防止Android键盘弹出并使EditTexts为只读。 When I select buttons on my keyboard, the values of the buttons' text is displayed in the EditText field that I have assigned as focused (I do this using onTouch and setting a flag/boolean).
当我选择键盘上的按钮时,按钮文本的值将显示在已分配为焦点的EditText字段中(我使用onTouch并设置了一个标志/布尔值来执行此操作)。 If the flag
mIsNameSelected = true
, then the name EditText is considered focused, otherwise the email EditText is considered focused. 如果标志
mIsNameSelected = true
,则名称EditText被认为是关注的,否则电子邮件EditText被认为是关注的。 Here's my dilemma; 这是我的困境;
First side-effect of this implementation is that when my text is longer than the space allotted to the EditText field, the text disappears to the right. 此实现的第一个副作用是,当我的文本长于分配给EditText字段的空间时,文本将消失在右侧。 I want to ellipsize the beginning as if the EditText was functioning normally but I think by setting it to
isFocusable(false)
I have eliminated it's ability to do this. 我想省略开头,就像EditText正常运行一样,但是我认为通过将其设置为
isFocusable(false)
我已经消除了它执行此操作的能力。 Is there a work-around? 有解决方法吗?
Second, to add a visual cue that the user has selected one of the EditTexts, I want to change the color of the underline on the EditText, not the text-link. 其次,要添加一个视觉提示,表明用户选择了一个EditText,我想更改EditText而不是文本链接上的下划线的颜色。 The text link doesn't show up because the of the not-focusable attribute setting.
由于无法聚焦的属性设置,该文本链接未显示。 I know there must be a way to change the EditText's style programmatically, which is what I'm looking for.
我知道必须找到一种以编程方式更改EditText样式的方法,这就是我要寻找的。
Here is a diagram: 这是一个图:
The buttons call the type()
method in XML 按钮调用XML中的
type()
方法
And here are the relevant methods (type() is called in XML): 这里是相关的方法(在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));
}
}
}
Set 组
android:windowSoftInputMode="stateAlwaysHidden"
in the activity xml of your manifest. 在清单的活动xml中。 You can have your views as focusable without the keyboard popping up then.
您可以在不弹出键盘的情况下使视图具有焦点。
https://developer.android.com/guide/topics/manifest/activity-element.html https://developer.android.com/guide/topics/manifest/activity-element.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.