防止在 EditText 上输入键,但仍将文本显示为多行

2023-02-26移动开发问题
4

本文介绍了防止在 EditText 上输入键,但仍将文本显示为多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

如何在 Android 上制作 EditText,这样用户可能不会输入多行文本,但显示仍然是多行的(即,有自动换行而不是向右移动的文本)?

How do I make an EditText on Android such that the user may not enter a multi-line text, but the display is still multi-line (i.e. there is word-wrap instead of the text going over to the right)?

类似于内置短信应用,我们不能输入换行符,但文本显示为多行.

It's similar to the built-in SMS application where we can't input newline but the text is displayed in multiple lines.

推荐答案

我将子类化小部件并覆盖键事件处理以阻止 Enter 键:

I would subclass the widget and override the key event handling in order to block the Enter key:

class MyTextView extends EditText
{
    ...
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if (keyCode==KeyEvent.KEYCODE_ENTER) 
        {
            // Just ignore the [Enter] key
            return true;
        }
        // Handle all other keys in the default way
        return super.onKeyDown(keyCode, event);
    }
}

这篇关于防止在 EditText 上输入键,但仍将文本显示为多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

恢复游戏 cocos2d
Resume game cocos2d(恢复游戏 cocos2d)...
2024-08-12 移动开发问题
6

cocos2d如何开启多点触控?
How to enable multi touch in cocos2d?(cocos2d如何开启多点触控?)...
2024-08-12 移动开发问题
4

UIGestureRecognizers (iPhone, Cocos2d) 的多种手势
Multiple Gestures for UIGestureRecognizers (iPhone, Cocos2d)(UIGestureRecognizers (iPhone, Cocos2d) 的多种手势)...
2024-08-12 移动开发问题
5

多点触控检测差异化 - Cocos2d for iPhone
Multi-touch detecting amp; differentiating - Cocos2d for iPhone(多点触控检测差异化 - Cocos2d for iPhone)...
2024-08-12 移动开发问题
1

如何在 COCOS2d Android 中使用 CClistview?
How can I use CClistview in COCOS2d Android?(如何在 COCOS2d Android 中使用 CClistview?)...
2024-08-12 移动开发问题
5

cocos2d v3 支持多屏幕分辨率?
Multiple screen resolution support in cocos2d v3?(cocos2d v3 支持多屏幕分辨率?)...
2024-08-12 移动开发问题
9