如何在edittext中的每个字符下划线? [英] how can i underline each character in edittext?
问题描述
我正在尝试在android中编写像hangman这样的猜词游戏. 假设这个词是否为苹果".我想显示5个下划线,以告诉玩家该单词的长度为5个字符,因此应该像下面这样显示/填充
I'm trying to write a word guessing game like hangman in android. let's say if the word is 'apple'. I want to display 5 underlines to tell the player that the word is 5 chars long, and they should be displayed/filled like below
我想不出一种简单的方法.我发现此与我所寻找的完全相同,但是没有给出答案,并且以某种方式获得了很多反对意见.
i cant think of a way to do so easily. i found this to be exact the same what Im looking for but there's no answer given and somehow got a lot of downvotes.
我最初的想法是创建带有下划线的edittext作为提示,并在所有字符之间循环.这将基于多少个字符创建#edittext,但是一旦填充了char,下划线就会消失,并且当char被删除/填充时,我需要创建指向上一个/下一个edittext的链接.
my initial thought is creating edittext with underline to be the hint and loop through all the characters. this will create # of edittext based on how many characters, but underline will be gone once a char is filled, and i need to create links to the previous/next edittext when the char is deleted/filled.
关于如何实现此目标的任何想法?还是有一个库可以使用?非常感谢!!
any idea on how to accomplish this? or is there a lib to use? Thank you so much!!
推荐答案
我认为您不需要使用edittext来实现它.您可以添加一个水平线性布局,该布局在布局内部不包含任何视图.
i think you dont need to use edittext for achieve it. You can add a horizontal linearlayout which is do not contain any view inside in your layout.
然后您可以创建一个布局,该布局将作为视图填充在水平linearlayout中. (该名称可以是row_word)此布局将在每个edittext下方包括下划线.您可以使用linearlayout实现它.
And than you can create a layout which will be populate in the horizontal linearlayout as a view. (thats name can be row_word) This layout will be include the underline below every edittext. You can use linearlayout for achive it.
在您的片段中,您可以编写一个填充方法,如下所示:
In your fragment you can write a populate method which likes below:
public class Character
{
public String char;
public boolean isFilled;
}
ArrayList<Character> words = new ArrayList();
words.addAll(_yourWords);
linearlayout.removeAllViews();
for(int i = 0 ; i < words.size(); i++)
{
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.row_word, horizontalLinearLayout, false);
EditText editText = v.findViewById(R.id.textView);
//you must declare focusable and focusableintouchmode values false in
//the your row_word
if(!word.isFilled)
{
edittext.setFocusable(true);
edittext.setFocusableontouhcmode(true);
editText.requestFocus();
}
else{
edittext.setFocusable(false);
edittext.setFocusableontouhcmode(false);
edittext.clearfocus();
}
edittex.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
populate();
}
.
.
.
.
.
.
horizontalLinearLayout.addView(v);
}
如果要在row_word中使用textview,请在活动中重写以下方法以从键盘获取按下的键值(当然使用界面).否则,您不需要使用以下代码.
if you want to use textview in your row_word then override the below method in your activity for get the pressed key value from the keyboard(With using interface of course). Otherwise you dont need to use below codes.
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
Log.i("key pressed", String.valueOf(event.getKeyCode()));
return super.dispatchKeyEvent(event);
callBack.setValue(String.valueOf(event.getKeyCode())
}
您的界面就像:
public interface pressedKeyCallback {
void onKeyPressed(String pressedKey);}
用于显示键盘:
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm != null){
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
}
这篇关于如何在edittext中的每个字符下划线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!