具有正确重力和单行的提示和文本视图

发布于 2024-10-10 23:44:49 字数 1299 浏览 5 评论 0原文

我打开了一个错误,但我想知道是否有人遇到过这个问题并知道解决方法。 如果您定义一个带有提示的文本视图,请给它正确的重力(android:gravity =“right”),然后如果您定义android:singleLine = true或android:maxLines =“1”或android:scrollHorizo​​natally =“true”你没有看到提示。删除右侧重力会将提示返回到左侧,删除我上面提到的所有树参数会将提示放在右侧。我希望我的提示在右侧,但我需要一条水平线...

这是不显示提示的示例布局:

<?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%,我很确定这是一个错误,我没有看到单行和提示之间的联系......更重要的是,提示在 TextView 中拥有自己的布局(mLayout 和 mHintLayout 都存在,在 onDraw 中,如果文本长度为 0 mHintLayout 如果 mHint 不为 null被使用)。

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>

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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

So要识趣 2024-10-17 23:44:49

您尝试过android:ellipsize="start"吗?过去,当我希望提示和 EditText 居中时,这对我来说非常有用。

Did you try android:ellipsize="start"? This has worked great for me in the past when I've wanted my hint and EditText to be centered.

是伱的 2024-10-17 23:44:49

看起来你对这个问题的看法完全正确;我尝试使用您的示例布局并看到了同样的问题。我假设是您的错误报告。

最简单的解决方案是仅更改布局,但这可能不是您想要做的。我的第一个解决方法是尝试不在 XML 中设置这三个属性中的任何一个,然后在 Java 中设置它们。如果这不起作用...

一种选择是通过扩展 EditText 类并尝试自行修复布置提示的代码来模仿提示,或者通过重写 onDraw 方法来创建提示,或者简单地通过将常规 TextView 重叠在 EditText 之上,然后您可以手动显示/隐藏它。您甚至可以让视图检查它是否为空,如果是,则将文本设置为提示文本并更改颜色。当视图获得焦点时,检查其文本是否等于您的提示,如果是,则删除文本并将颜色更改回来。

另一种可能的解决方法有点“hacky”,那就是放弃导致问题的三个属性,但尝试手动阻止创建换行符。您需要为您的 EditText 创建一个 OnKeyListener,如下所示:

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) 以避免显示返回键。仍然可以通过粘贴或其他方法在文本字段中创建换行符,因此为了安全起见,您还需要在提交表单时解析并删除换行符。就水平滚动而言,这也不太可能达到您想要的效果。

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.

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...

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.

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;
        }
    });

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.

烟花易冷人易散 2024-10-17 23:44:49

使用这些属性与提示和单线...你可以改变重力!

            android:gravity="right"
            android:ellipsize="start"
            android:imeOptions="actionNext"             
            android:singleLine="true"

use these properties with hint and single line...u can chnge gravity!!

            android:gravity="right"
            android:ellipsize="start"
            android:imeOptions="actionNext"             
            android:singleLine="true"
一杆小烟枪 2024-10-17 23:44:49

注意到对我来说已经足够好了。当我设置Gravity.right时,光标总是在右侧,无法放置在单词中间。

我尝试了一种不同的方法 - 当没有文本时将重力设置在右侧(或左侧,如果它适合您),并让 android 在用户输入内容后决定最佳方向

这对我有用:

  1. create TextWatcher

    私有静态类 FilterTextWatcher 实现 TextWatcher {
    
        私有 WeakReference; m活动;
    
        公共 FilterTextWatcher(活动活动){
            mActivity = new WeakReference(活动);
        }
    
        公共无效afterTextChanged(可编辑的s){
        }
    
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }
    
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (mActivity.get() == null)
                返回;
    
            EditText searchTxtBx = mActivity.get().mSearchTxtBx;
            if (searchTxtBx.getText().toString().length() == 0)
                searchTxtBx.setGravity(Gravity.RIGHT);
            别的
                searchTxtBx.setGravity(0);
        }
    }
    
  2. 将其用作类成员

    private TextWatcher mFilterTextWatcher = new FilterTextWatcher(this);
    
  3. mSearchTxtBx.addTextChangedListener(mFilterTextWatcher);
    
  4. onDestroy()中:

    mSearchTxtBx.removeTextChangedListener(mFilterTextWatcher);
    mFilterTextWatcher = null;
    

Noting worked good enough for me. When I set Gravity.right, the cursor was always on the right and couldn't be placed in the middle of the word.

I tried a different approach - put the set the gravity the the right when there is no text (or left, if it works for you) and let android decide the best direction once the user entered something

This worked for me:

  1. create TextWatcher class

    private static class FilterTextWatcher implements TextWatcher {
    
        private WeakReference<Activity> mActivity;
    
        public FilterTextWatcher(Activity activity) {
            mActivity = new WeakReference<Activity>(activity);
        }
    
        public void afterTextChanged(Editable s) {
        }
    
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }
    
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (mActivity.get() == null)
                return;
    
            EditText searchTxtBx = mActivity.get().mSearchTxtBx;
            if (searchTxtBx.getText().toString().length() == 0)
                searchTxtBx.setGravity(Gravity.RIGHT);
            else
                searchTxtBx.setGravity(0);
        }
    }
    
  2. use it as class member

    private TextWatcher mFilterTextWatcher = new FilterTextWatcher(this);
    
  3. in onCreate():

    mSearchTxtBx.addTextChangedListener(mFilterTextWatcher);
    
  4. in onDestroy():

    mSearchTxtBx.removeTextChangedListener(mFilterTextWatcher);
    mFilterTextWatcher = null;
    
人心善变 2024-10-17 23:44:49

您认为我对这个问题的解决方案怎么样?

editText.setOnFocusChangeListener(new OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus == false && StringUtils.isNotBlank(editText.getText().toString())) {
                editText.setGravity(Gravity.RIGHT);
            }
        }
    });

相应的 XML 文件:

<EditText android:id="@+id/the_text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="number" android:hint="@string/edit_text_prompt"/>

对我来说工作得很好:只有一行,没有可能的回车键,向我显示提示,当我在给出一些输入后离开该字段时,文本显示为右对齐。

What do you think about my solution to this problem?

editText.setOnFocusChangeListener(new OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus == false && StringUtils.isNotBlank(editText.getText().toString())) {
                editText.setGravity(Gravity.RIGHT);
            }
        }
    });

And the corresponding XML File:

<EditText android:id="@+id/the_text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="number" android:hint="@string/edit_text_prompt"/>

Works fine for me: just one line, no enter-key possible, shows me the hint and when I leave the field after some input was given, the text appears right-aligned.

梦太阳 2024-10-17 23:44:49

它对我有用

android:hint="the hint text ..."
android:singleLine="true"
android:ellipsize="start"

当我添加:并在我的活动中添加:时

myedittext.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;
    }
});

it worked with me when I added:

android:hint="the hint text ..."
android:singleLine="true"
android:ellipsize="start"

and in my activity i added :

myedittext.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;
    }
});
强者自强 2024-10-17 23:44:49

当我的 TextView atrs 为:

android:singleLine="true"  
android:gravity="right"

当我尝试在该文本视图上链接文本视图或 setMovementMethod(LinkMovementMethod.getInstance()) 时,我注意到这个问题,文本就消失了。

android:ellipsize="start" 

解决了我的问题,因为我在我的应用程序中使用阿拉伯文本。

I noticed this issue when my TextView atrs are:

android:singleLine="true"  
android:gravity="right"

When I try to Linkify the textview or setMovementMethod(LinkMovementMethod.getInstance()) on that textview, the text is just gone.

android:ellipsize="start" 

solved my issue, because I use Arabic text in my app.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文