简体   繁体   English

在单击按钮时显示键盘,并在textview中显示一些文本

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

In my activity there is a button and a textview .What I am trying is when the user clicks on a button the keyboard shows up ,and whatever the user types there that text should be displayed in textview.I'm able to show keyboard on button click but can't get the text in textview. 在我的活动中有一个按钮和一个textview。我想做的是当用户单击一个按钮时,键盘就会显示出来,无论用户键入什么,都应该在textview中显示文本。单击按钮,但无法在textview中获取文本。 I tried textview.setFocusableInTouchMode (true); & textview.requestFocus (); 我尝试了textview.setFocusableInTouchMode (true); & textview.requestFocus (); textview.setFocusableInTouchMode (true); & textview.requestFocus (); on button click ,but still can't get the text. 在按钮上单击,但仍无法获取文本。

As suggested, I added edittext instead of textview and also added text changed listener on it , but not able to achieve what I want .The keyboard should appear on button click and then show the text in edittext . 如建议的那样,我添加了edittext而不是textview,并且还添加了更改了监听器的文本,但是无法实现我想要的功能。键盘应该在单击按钮时出现,然后在edittext中显示文本。

you can solve this in 2 ways 你可以用两种方法解决这个问题

method 1: 方法1:

add textChangeListener to your EditText, like this 像这样将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) {
        }
    });

method 2: 方法2:

use the only editText and set EditText background color same as your screen background color then your editText will look like a textView and automatically typed value will be there 使用唯一的editText并将EditText背景色设置为与屏幕背景色相同的颜色,然后editText看起来像textView并在其中自动键入值

like this : 像这样 :

android:background="your screen background color"

This might help you 这可能对您有帮助

You can use OnKeyListener for example, 例如,您可以使用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 
}

You can set the string to TextView . 您可以将字符串设置为TextView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM