Android如何使用单行输入和多行提示创建编辑文本 [英] Android how to create edit text with single line input and multiline hint

查看:156
本文介绍了Android如何使用单行输入和多行提示创建编辑文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望创建一个带有3-4行提示的编辑文本,单击该提示将允许用户仅输入一行文本. 我尝试使用

I am looking to create an edit text with a 3-4 line hint which on click, allows the user to enter only a single line of text. I have tried to use

android:singleLine = "true"

但这也限制了提示的长度.

But that limits the length of the hint as well.

如何为两者分配不同的行数?

How do I assign different number of lines for both ?

谢谢

推荐答案

您可以将TextWatcher添加到您的编辑文本中,并比较文本的长度. 如果为0,则显示提示,并以编程方式设置editText.setSingleLine(false).如果它大于0,则使用setSingleLine(true)限制editText.

You could add a TextWatcher to your edit text and compare the length of the text. If it is 0, then the hint is shown and you programmatically set editText.setSingleLine(false). If it is greater than 0, you restrict the editText with setSingleLine(true).

伪代码:

    public void some(){
    final EditText t = findViewById(someName);
    t.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            t.setSingleLine(t.getText().toString().length() > 0);
        }
    });
}

这篇关于Android如何使用单行输入和多行提示创建编辑文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆