如何在edittext中为每个字符加下划线?

use*_*027 6 android android-edittext

我正在尝试在android中编写一个像刽子手这样的猜词游戏。假设这个词是“苹果”。我想显示 5 个下划线来告诉玩家这个词是 5 个字符长,它们应该像下面一样显示/填充

在此处输入图片说明

我想不出一种方法可以轻松做到这一点。我发现与我正在寻找的完全相同,但没有给出答案并且不知何故得到了很多反对票。

我最初的想法是创建带有下划线的 edittext 作为提示并循环遍历所有字符。这将根据字符数创建# of edittext,但是一旦填充了字符,下划线就会消失,当删除/填充字符时,我需要创建指向上一个/下一个编辑文本的链接。

关于如何实现这一点的任何想法?或者有没有可以使用的库?非常感谢!!

Ali*_*lli 3

我认为你不需要使用 edittext 来实现它。您可以添加一个水平线性布局,它在布局中不包含任何视图。

然后您可以创建一个布局,该布局将作为视图填充在水平线性布局中。(名称可以是 row_word)此布局将在每个编辑文本下方包含下划线。您可以使用线性布局来实现它。

在您的片段中,您可以编写一个填充方法,如下所示:

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);

    }
Run Code Online (Sandbox Code Playgroud)

如果您想在 row_word 中使用 textview,则在您的活动中重写以下方法以从键盘获取按下的键值(当然使用接口)。否则您不需要使用下面的代码。

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    Log.i("key pressed", String.valueOf(event.getKeyCode()));
    return super.dispatchKeyEvent(event);
    callBack.setValue(String.valueOf(event.getKeyCode())

}
Run Code Online (Sandbox Code Playgroud)

你的界面是这样的:

public interface pressedKeyCallback {

void onKeyPressed(String pressedKey);}
Run Code Online (Sandbox Code Playgroud)

用于显示键盘:

InputMethodManager imm = (InputMethodManager)
                                 getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm != null){
        imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
    }
Run Code Online (Sandbox Code Playgroud)