Android中EditText无法完全显示

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android中EditText无法完全显示相关的知识,希望对你有一定的参考价值。

如图,代码如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<!-- name -->
<TextView
android:id="@+id/name"
android:layout_width="80sp"
android:layout_height="wrap_content"
android:layout_marginTop="5px"
android:text="姓名" />

<EditText
android:id="@+id/name_e"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/name"
android:layout_alignTop="@+id/name"
android:layout_toRightOf="@+id/name"
android:hint="请输入文字"/>

</RelativeLayout>

参考技术A android:layout_alignBottom="@+id/name"
android:layout_alignTop="@+id/name"
这两句话去掉就好了。百度下一些属性的用法。

动手敲打码是编程好的开始。也要学会百度追问

谢谢,不过我的想法是把EditText的框体大小变成和前面的TextView大小一样。您看有没有方法可以达到那样的效果?

追答

是要宽度和大小都一样的吗? 如果是下面两个一个是宽度,一个是高度。设置一样就可以一样值,例100dp,的大小了。

android:layout_width="wrap_content"
android:layout_height="wrap_content"

本回答被提问者采纳
参考技术B 估计是你设置了padding 内边距导致的
或者你写死了EditText 宽度,建议把layout贴出来
参考技术C android:layout_width="80sp" TextView设置了高度。然后EditText:
android:layout_alignBottom="@+id/name"
android:layout_alignTop="@+id/name"
相当于也给Edittext设置了高度,所以会显示不全。追问

width不是宽度吗?

参考技术D 你要达到什么效果啊……TextView显示在左边,然后EditText占满其余的宽度?追问

嗯,并且使EditText得框体大小和TextView一样大- -理想状况是这样的。。。

追答

不要用相对布局,用线性布局。orientation为horizontal,TextView的layout_width设为wrap content,Edit Text的layout_width=0dp, layout_weight设为1。然后设置两个的layout_gravity为center_vertical

android-edittext键盘不显示

【中文标题】android-edittext键盘不显示【英文标题】:android - edittext keyboard not showing 【发布时间】:2021-01-27 14:43:33 【问题描述】:

我正在以编程方式创建 edittext。但我无法在编辑文本中输入任何内容。我已经涵盖了该网站上的所有相关答案,但没有一个对我有用。也许我遗漏了一些小细节。如果我能得到一些帮助会很有帮助。

动态创建编辑文本的代码:

EditText editText = new EditText(dt.c);
editText.setBackground(context.getResources().getDrawable(R.drawable.rectangle_answer));
editText.setTextColor(ContextCompat.getColor(dt.c, R.color.md_black_1000));
editText.setTextSize(16);
editText.setTag(questionDetails[0].getOptionId());
editText.setFocusable(true);
editText.setHint(dt.gStr(R.string.enter_your_answer));
editText.requestFocus();
container.Add(editText);

我涵盖的一些答案:

How can I set the focus (and display the keyboard) on my EditText programmatically

Android - EditText made programmatically not showing keyboard

Android: How to open keyboard for editing EditText when click button?

Disable keyboard on EditText

How to disable the keyboard when I click on EditText?

【问题讨论】:

【参考方案1】:

我遇到了同样的问题,直到我在多年前的一篇有效的帖子中找到了答案。我会为将来可能遇到此问题的任何人重新发布这个奇怪的 hack。

在您的 xml 文件中,将一个虚拟的 edittext 添加到您的最顶层布局层,如下所示:

<EditText
            android:id="@+id/editTextFix"
            android:layout_
            android:layout_
            android:importantForAutofill="no"
            tools:targetApi="o"
            android:inputType="text"
            android:visibility="gone"/>

虚拟编辑文本将打开键盘,但您以编程方式创建的编辑文本仍具有焦点。

【讨论】:

感谢您的回复。但是您的建议并不是一个合适的解决方案,尽管我通过添加以下两行来喜欢我的问题。 editText.setFocusable(true); editText.setFocusableInTouchMode(true); 对于使用 Xamarin 的任何人,在布局中隐藏 EditText 是唯一适合我的解决方案。将 Focusable 设置设置为 true 并不能解决问题。

以上是关于Android中EditText无法完全显示的主要内容,如果未能解决你的问题,请参考以下文章

无法从edittext获取值到字符串

带有 android:hint 的 Android EditText 密码

单击EditText时如何隐藏键盘不显示

android-edittext键盘不显示

android - 在更改 EditText 值时无法更新列表视图

Android:如何在 Edittext 中完全禁用复制和粘贴功能