单击按钮显示键盘并在文本视图中显示一些文本

showing the keyboard on a button click and showing some text in a textview

在我的 activity 中有一个按钮和一个文本视图。我正在尝试的是当用户单击一个按钮时键盘会出现,并且无论用户在那里键入什么文本都应该显示在 textview.I能够在单击按钮时显示键盘,但无法在文本视图中获取文本。我试过了 textview.setFocusableInTouchMode (true); & textview.requestFocus (); 单击按钮,但仍然无法获取文本。

按照建议,我添加了 edittext 而不是 textview,并在其上添加了文本更改侦听器,但无法实现我想要的效果。键盘应该在单击按钮时出现,然后在 edittext 中显示文本。

你可以通过两种方式解决这个问题

方法一:

将 textChangeListener 添加到您的 EditText,就像这样

yourEditText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            yourTextView.setText(charSequence);
        }

        @Override
        public void afterTextChanged(Editable editable) {
        }
    });

方法二:

使用唯一的 editText 并将 EditText 背景颜色设置为与您的屏幕背景颜色相同,然后您的 editText 将看起来像一个 textView 并且自动键入的值将在那里

像这样:

android:background="your screen background color"

这可能对您有所帮助

您可以使用 OnKeyListener 例如,

@Override
public boolean dispatchKeyEvent(KeyEvent keyEvent) 
{
    int keyAction = keyEvent.getAction();
    if(keyAction == KeyEvent.ACTION_DOWN) //any action event you prefer
    {
       char pressedKey = (char) keyEvent.getUnicodeChar();
    }
    //append the char to the string variable
    //return 
}

您可以将字符串设置为TextView