【问题标题】:showing the keyboard on a button click and showing some text in a textview在按钮单击时显示键盘并在文本视图中显示一些文本
【发布时间】:2018-09-16 01:30:47
【问题描述】:

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

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

【问题讨论】:

  • 声明一个edittext并将其可见性设置为不可见,然后使用addTextChangedListener 更新您的文本视图
  • 为什么要使用 TextView?您可以使用 EditText。
  • @NiranjPatel 我只想显示用户使用 textview 键入的文本
  • 你应该使用 EditText

标签: android textview focusable


【解决方案1】:

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

方法一:

将 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"

这可能对你有帮助

【讨论】:

    【解决方案2】:

    例如,您可以使用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

    【讨论】:

      猜你喜欢
      • 2013-04-11
      • 2017-01-10
      • 1970-01-01
      • 1970-01-01
      • 2016-04-14
      • 1970-01-01
      • 2018-08-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多