提示和TextView中右重力和一个单线 [英] hint and textview with right gravity and a singleline

查看:102
本文介绍了提示和TextView中右重力和一个单线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开了一个错误,但我想知道是否有人遇到过这个问题,知道一种解决方法。 如果定义里面它的提示文本视图,赋予它的权利重力(安卓重力=右),那么,如果你定义的android:单线=真或Android:MAXLINES =1或Android:scrollHorizo​​natally =真你看不到提示。删除右边的重力返回提示左侧,删除所有的树PARAMS我上述所说的提示右侧。我希望我的暗示就没事了,但我需要一个水平线...

I've opened a bug but i was wondering if anyone encountered this issue and knows a workaround. If you define a text view with a hint inside it, give it right gravity (android:gravity="right") then if you define android:singleLine=true or android:maxLines="1" or android:scrollHorizonatally="true" you don't see the hint. removing the right gravity returns the hint to the left side, removing all the tree params i mentioned above puts the hint on the right side. i want my hint on the right, but i need a single horizontal line...

下面的示例布局不显示提示:

here's the sample layout that doesn't show the hint:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="5dp">
            <EditText android:layout_width="fill_parent"
                android:layout_gravity="center_vertical|right"
                android:layout_height="wrap_content"
                android:layout_margin="6dp"
                android:textSize="16sp"
                android:paddingRight="5dp"
                android:id="@+id/c"
                android:gravity="right"
                android:hint="hello!!!"
                android:scrollHorizontally="true"
                android:maxLines="1"
                android:singleLine="true"/>
</LinearLayout>

我查了1.6和2.1模拟器,它再现了100%,我是prettysure这是一个错误,我没有看到一行与提示之间的连接....更重要的是在暗示了它的在TextView中自己的布局(mLayout和mHintLayout既存在,在OnDraw中如果文本长度为0 mHintLayout如果mHint不使用空)。

i checked on 1.6 and 2.1 emulators and it reproduces 100%, i'm prettysure it's a bug, i don't see the connection between single line and the hint.... what's more the hint got it's own layout in the TextView (mLayout and mHintLayout both exists, in onDraw if the text length is 0 mHintLayout if mHint is not null is used).

推荐答案

看起来你是完全正确的问题;我试着用你的榜样布局玩,看到了同样的问题。我想是你的错误报告。

Looks like you're exactly right with the issue; I tried playing with your example layout and saw the same issue. I assume this is your bug report.

最简单的解决方法就是改变你的布局,但是这可能不是你想要做什么。在工作​​我第一次尝试各地将尝试不设置任何XML这三个属性,然后设置他们的Java。如果还是不行......

The easiest solution is to just change your layout, but that's probably not what you want to do. My first attempt at a work around would be to try not setting any of those three attributes in XML and then setting them in Java. If that doesn't work...

一种选择是要么扩展的EditText类,并尝试修复code,规定了暗示自己模仿的暗示,或者通过简单的重叠定期覆盖的OnDraw方法创建的暗示,或者的TextView的EditText上,然后您再进行手工显示/隐藏的顶部。你甚至可以认为检查,如果它是空的,如果是这样的文字设置为您的提示文本,改变颜色。当视图获得焦点,检查它的文本等于你的提示,如果是这样,删除文本,改变颜色了。

One option is to mimic the hint by either extending the EditText class and attempting to fix the code that lays out the hint yourself, or by overriding the onDraw method to create the hint, or perhaps by simply overlapping a regular TextView on top of the EditText, which you then show/hide manually. You could even have the view check if it's empty, and if so set the text to your hint text and change the color. When the view gains focus, check if its text is equal to your hint and, if so, remove the text and change the color back.

另一个可能的解决方法,就是多一点哈克是离开关闭导致问题的三个属性,而是尝试手动prevent正在创建一个换行符。你需要创建一个OnKeyListener为您的EditText,是这样的:

Another possible workaround that's a bit more "hacky" is to leave off the three attributes that cause problems, but try to manually prevent a newline from being created. You'd need to create an OnKeyListener for your EditText, something like this:

editText.setOnKeyListener(new OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                // do nothing
                return true;
            }
            return false;
        }
    });

您也想叫 editText.setImeOptions(EditorInfo.IME_ACTION_NEXT),以避免显示的返回键。它仍然可能通过粘贴进去或者一些其他的方法来创建你的文本字段换行,所以你也想解析,当表单提交为了安全起见除去新行。这也不太可能做你想做的,只要水平滚动的东西。

You would also want to call editText.setImeOptions(EditorInfo.IME_ACTION_NEXT) to avoid showing the return key. It still may be possible to create a newline in your text field by pasting into it or perhaps some other method, so you would also want to parse and remove newlines when the form is submitted just to be safe. This is also not likely to do what you want as far as horizontal scrolling.

这篇关于提示和TextView中右重力和一个单线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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