how can i underline each character in edittext?

不问归期 提交于 2019-12-06 14:04:51

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.

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

    }

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

}

Your interface is like:

public interface pressedKeyCallback {

void onKeyPressed(String pressedKey);}

for showing keyboard:

InputMethodManager imm = (InputMethodManager)
                                 getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm != null){
        imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
    }

The EditText has an underline by default, so you could just edit the colors in your style to make it darker. Add these in your style.

<item name="android:textColorSecondary">@android:color/black</item>
<item name="colorAccent">@android:color/black</item>

I'd also add this so that you don't have that blinking cursor.

editText.setCursorVisible(false);

I think you should you the built in hint feature of edittext. Check this out.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!