textview 相关问题

Android小部件,向用户显示文本,并可选择允许他们编辑文本。 TextView是一个完整的文本编辑器,但基本类配置为不允许编辑

Android获取widget内的textView值

有没有办法从小部件内的textView项获取值?我的小部件布局中有 2 个 textView,在小部件更新时我想为其中之一设置新值,但我不想更改值...

回答 2 投票 0

通过xml从AutoCompleteTextView中删除下划线

我有这样的观点: 我有这样的观点: <AutoCompleteTextView android:id="@+id/search_bar" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignBottom="@+id/rect_search" android:layout_alignLeft="@+id/rect_search" android:layout_alignRight="@+id/rect_search" android:layout_alignTop="@+id/rect_search" android:hint="@string/search_bar" android:textSize="20sp" /> 如您所见,该视图进入另一个视图 rect_search,即 ImageView。 现在我注意到 AutoCompleteTextView 小部件底部有一个默认下划线,我将删除它。有可能吗? 只需输入: android:background="@android:color/transparent" 在你的视野中。 编辑:正如@kiya所说,现在更好用 android:backgroundTint="@android:color/transparent" 因为它保持视图尺寸。 android:inputType="textPhonetic" 这也能正常工作 android:inputType="textNoSuggestions" 如果您想删除下划线并保持大小,请使用以下代码: android:backgroundTint="@android:color/transparent" 我做了很多尝试来构建一个具有正确颜色的下拉菜单,并聚焦具有圆角的材质外观选项。 我会分享它来帮助其他人,结果会是这样的: 非对焦模式下: 在聚焦模式下并激活下拉菜单: 目前唯一的问题是,如果有人可以通过某种方式集中下拉菜单(例如使用键盘上的 TAB 按钮),他/她可以在文本框中输入内容,但在触摸模式下,就不会有问题 这是布局中应使用的 XML: <com.google.android.material.textfield.TextInputLayout style="@style/CustomDropDownLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="50dp" android:layout_marginEnd="50dp" android:padding="5dp" app:boxCornerRadiusBottomEnd="18dp" app:boxCornerRadiusTopEnd="18dp" app:boxCornerRadiusBottomStart="18dp" app:boxCornerRadiusTopStart="18dp" app:boxStrokeWidth="0dp" > <AutoCompleteTextView android:background="@drawable/drop_down_background" android:id="@+id/userNameDropdown" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="none" /> </com.google.android.material.textfield.TextInputLayout> 将自定义样式放入:res\values\customstyles.xml文件: <?xml version="1.0" encoding="utf-8"?> <resources> <style name="CustomDropDownLayout" parent="Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu"> <item name="helperTextTextAppearance">@style/TextInputLayoutHelperStyle</item> <item name="errorTextAppearance">@style/TextInputLayoutErrorStyle</item> <item name="hintTextAppearance">@style/TextInputLayoutHintStyle</item> <item name="boxStrokeColor">@color/text_input_layout_dropdown_hint_color</item> </style> <style name="TextInputLayoutErrorStyle" parent="TextAppearance.Design.Error"> <item name="fontPath">fonts/Yekan-Bakh-Fat.ttf</item> </style> <style name="TextInputLayoutHintStyle" parent="TextAppearance.Design.Hint"> <item name="fontPath">fonts/Yekan-Bakh-Fat.ttf</item> </style> <style name="TextInputLayoutHelperStyle" parent="TextAppearance.Design.HelperText"> <item name="fontPath">fonts/Yekan-Bakh-Fat.ttf</item> </style> <style name="TextLabel" parent="TextAppearance.Design.Hint"> <item name="android:textSize">20sp</item> <item name="android:textColor">@color/input_layout_not_focused</item> </style> </resources> 并将此颜色放入单个文件中,例如 res\color\text_input_layout_dropdown_box_color.xml: <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="@color/colorPrimary" android:state_focused="true" /> <item android:color="@color/colorPrimary" android:state_hovered="true" /> <item android:color="@color/colorPrimary" android:state_enabled="false" /> <item android:color="@color/input_layout_not_focused" /> <!-- unfocused --> </selector> 还有另一个名为 \res\color\text_input_layout_dropdown_hint_color.xml 的文件 <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="@color/colorPrimary" android:state_focused="true" /> <item android:color="@color/colorPrimary" android:state_hovered="true" /> <item android:color="@color/colorPrimary" android:state_enabled="false" /> <item android:color="@color/input_layout_not_focused" /> <!-- unfocused --> </selector> 并将文件放入可绘制对象中res\drawable\drop_down_background.xml: <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#FFFFFF" /> <corners android:radius="20dp" /> <stroke android:width="2dp" android:color="@color/text_input_layout_dropdown_box_color" /> </shape> 并使用这样的自定义适配器: ArrayAdapter dropDownAdpater = new ArrayAdapter<>(this, R.layout.dropdown_menu_popup_item, users); userNameDropDownMenuAutoCompleteTextView.setAdapter(dropDownAdpater); //set first item as selected one if (users != null && users.size() > 0) { userNameDropDownMenuAutoCompleteTextView.setText(dropDownAdpater.getItem(0).toString(), false); } userNameDropDownMenuAutoCompleteTextView.setDropDownBackgroundResource(R.drawable.drop_down_background); userNameDropDownMenuAutoCompleteTextView.setOnTouchListener((view, motionEvent) -> { userNameDropDownMenuAutoCompleteTextView.showDropDown(); return true; }); 最后在文件中的布局中添加一个文件res\layout\dropdown_menu_popup_item.xml: <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="16dp" android:ellipsize="end" android:maxLines="1" android:textAppearance="?attr/textAppearanceSubtitle1"/> 如果要删除下划线并保持大小,请使用以下代码:app:boxStrokeWidth="0dp" => android:id="@+id/textInputLayout2" android:layout_width="345dp" android:layout_height="65dp" android:background="color/transparent" android:backgroundTint="@color/transparent" app:endIconMode="dropdown_menu" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:boxBackgroundColor="@color/transparent" android:scrollIndicators="none" app:boxStrokeWidth="0dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:startIconDrawable="@drawable/people" app:startIconTint="@color/btnbackground"> <AutoCompleteTextView android:id="@+id/autotext" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/button_corner_border" android:inputType="text" android:textColor="@color/darkbringle" /> </com.google.android.material.textfield.TextInputLayout>

回答 6 投票 0

我面临与 Android 中弃用 singleLine 属性相关的问题

虽然 singleLine 已经被弃用很长一段时间了,但我还没有找到某些效果的合适替代品。例如,考虑以下场景: 虽然 singleLine 已经被弃用很长一段时间了,但我还没有找到某些效果的合适替代品。例如,考虑以下场景: <TextView android:layout_width="100dp" android:layout_height="wrap_content" android:ellipsize="end" android:singleLine="true" android:text="aaaaaaa\nbbbbbbb\ncccccc" /> 我想要一个单行文本视图,它会在末尾使用省略号 (...) 截断文本。 此外,我需要替换换行符( )的风格类似于空间。 想要的效果如下图所示: 您能否指导我如何使用其他 XML 属性达到相同的效果?请注意,我更喜欢不涉及编写代码的解决方案,因为这会违背最初的目的。 你必须编写代码。没有其他方法可以做到这一点。最简单的方法是在字段上设置一个 TextWatcher,每当设置新字符串时,替换所有字符串 在带有空格的字符串中,如果不存在则在末尾附加一个省略号(使用省略号 unicode 字符,这样他们就不能在点之间放置东西)。

回答 1 投票 0

样式上的按位运算(textview 字体)

我编写了为文本视图设置/取消设置粗体和斜体样式的代码。 它运行完美,但我的 Android Studio 遇到了一些问题。 样式 &= (int) ~Typeface.BOLD; - Android Studio 告诉我,

回答 1 投票 0

Android:TextView透明png作为背景资源不起作用

我在 CardView 中有一个简单的 TextView (充当按钮): 我在 CardView 中有一个简单的 TextView(充当按钮): <androidx.cardview.widget.CardView android:id="@+id/cvTestType" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="20dp" app:cardCornerRadius="5dp" app:cardElevation="10dp"> <TextView android:id="@+id/tvTestType" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:textStyle="bold" android:textSize="15sp" /> </androidx.cardview.widget.CardView> 我正在尝试为按钮设置透明的 png 背景,如下所示: private fun TextView.setTestTypeTextView(testType: TestTypeDTO) { val buttonFontSize = this.context.prefsByScreenDensity()[3] this.height = buttonsHeight this.text = testType.testTypeName.uppercase(Locale.getDefault()) this.setTextSize(TypedValue.COMPLEX_UNIT_SP, buttonFontSize.toFloat()) this.setTextColor(Color.parseColor(props.appearance.colors.appColor)) this.setBackgroundResource(R.drawable.button_act_background) } 问题是,从截图中可以看出,使用透明png作为背景是行不通的,它只是将按钮显示为白色,根本没有背景,只有在使用非透明背景时才有效. PNG 是使用 Gimp 创建(并导出)的,使用 Android Studio 预览可以完美地看到它。 有什么帮助吗? 编辑1: 尝试了不同的透明PNG,发现使用另一个PNG我可以看到绘图,但背景仍然是白色。 根本原因是您看到的 CardView 背景似乎与您的主题一样是白色的。您可以使用 app:cardBackgroundColor="@color/yourColor" 进行更改。

回答 1 投票 0

TextView 中动态文本颜色更改的最有效方法

我想用计时器多次更改文本部分的颜色。 最简单的方法是这样的: SpannableStringBuilder ssb = new SpannableStringBuilder(mainText); ForegroundColorSpan 跨度 = 新

回答 4 投票 0

Android中如何将图标垂直对齐到textview第一行的中心

我想放置左侧图标(请参阅下图),垂直居中于 TextView 中文本的第一行。我找不到方法,我在文本视图、重力和约束上尝试了drawableLeft...

回答 2 投票 0

HTML 格式字符串插入 TextViews 和 EditText

我有一个数据库,它将存储 HTML 标记文本以保留 EditText 中的格式信息。我使用 HTML.toHtml(EditText.getText) 创建此字符串。我注意到这个方法包含了任何内容

回答 2 投票 0

如何在android上的TextVIew上实现5秒内从0到600的数字递增动画

我计划在一定秒内通过动画实现textView上的整数从0增加到某个值。 例如,在 textview 上显示将数字从 0 增加到 600 的动画,持续 5 秒

回答 5 投票 0

从 Firebase Firestore 检索上次插入的集合名称 |单击“添加客户端”按钮后,在 TextView 中显示上次插入的集合名称

在我的 Android 应用程序中,我有一个 MainActivity 作为主要基础,其中有一个标有“添加客户端详细信息”的按钮。单击此按钮后,用户应导航到新活动

回答 1 投票 0

如何从屏幕上删除以编程方式创建的文本视图?

在我的例程(generateView())中,我正在创建一组排列在约束布局(reglette)中的textView(实际上是字母图块)。为了创建文本视图,我使用表达式 'val childView =

回答 1 投票 0

Kotlin:如何使用 Kotlin 在 Android 中获取文本并将其设置到 TextView?

我想在单击它时更改 TextView 的文本。 我的代码: val text: TextView = findViewById(R.id.android_text) as TextView 文本.setOnClickListener { text.setText(getString(R.string....

回答 14 投票 0

更改 TextView 中一个单词的文本颜色

我正在寻找一种方法来更改 Activity 中 TextView 中单个单词的文本颜色。 例如,这样: String first = "这个词是"; 下一个字符串=“红色” TextView t...

回答 11 投票 0

Android 按钮或 TextView 边框以编程方式,而不使用 setBackgroundDrawable 方法

我正在寻找一种以编程方式为文本视图或按钮添加边框而不使用 setBackgroundResource 方法的方法。 我在这里试图实现的目标是改变背景...

回答 3 投票 0

以编程方式在 TextView 中设置左侧可绘制对象

我这里有一个xml中的textView。 我这里有一个xml中的textView。 <TextView android:id="@+id/bookTitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:drawableLeft="@drawable/checkmark" android:gravity="center_vertical" android:textStyle="bold" android:textSize="24dip" android:maxLines="1" android:ellipsize="end"/> 如你所见,我在 xml 中设置了 DrawableLeft。 我想更改代码中的可绘制对象。 有办法去做这件事吗?或者在代码中为文本视图设置drawableLeft? 您可以使用 setCompoundDrawablesWithIntrinsicBounds(int left, int top, int right, int Bottom) 在不需要图像的地方设置 0 左侧可绘制示例: TextView textView = (TextView) findViewById(R.id.myTxtView); textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon, 0, 0, 0); 或者,您可以使用 setCompoundDrawablesRelativeWithIntrinsicBounds 来尊重 RTL/LTR 布局。 提示:每当您知道任何 XML 属性但不知道如何在运行时使用它时。只需转到开发人员文档中对该属性的描述即可。如果运行时支持,您将在那里找到相关方法。即 对于 DrawableLeft 使用 Kotlin: 您可以创建扩展函数或直接使用setCompoundDrawablesWithIntrinsicBounds。 fun TextView.leftDrawable(@DrawableRes id: Int = 0) { this.setCompoundDrawablesWithIntrinsicBounds(id, 0, 0, 0) } 如果需要调整drawable的大小,可以使用此扩展功能。 textView.leftDrawable(R.drawable.my_icon, R.dimen.icon_size) fun TextView.leftDrawable(@DrawableRes id: Int = 0, @DimenRes sizeRes: Int) { val drawable = ContextCompat.getDrawable(context, id) val size = resources.getDimensionPixelSize(sizeRes) drawable?.setBounds(0, 0, size, size) this.setCompoundDrawables(drawable, null, null, null) } 要变得真正奇特,请创建一个允许修改大小和/或颜色的包装器。 textView.leftDrawable(R.drawable.my_icon, colorRes = R.color.white) fun TextView.leftDrawable(@DrawableRes id: Int = 0, @DimenRes sizeRes: Int = 0, @ColorInt color: Int = 0, @ColorRes colorRes: Int = 0) { val drawable = drawable(id) if (sizeRes != 0) { val size = resources.getDimensionPixelSize(sizeRes) drawable?.setBounds(0, 0, size, size) } if (color != 0) { drawable?.setColorFilter(color, PorterDuff.Mode.SRC_ATOP) } else if (colorRes != 0) { val colorInt = ContextCompat.getColor(context, colorRes) drawable?.setColorFilter(colorInt, PorterDuff.Mode.SRC_ATOP) } this.setCompoundDrawables(drawable, null, null, null) } 从这里我看到方法setCompoundDrawablesWithIntrinsicBounds(int,int,int,int)可以用来做到这一点。 您可以使用以下任意一种方法在 TextView 上设置 Drawable: 1- setCompoundDrawablesWithIntrinsicBounds(int,int,int,int) 2- setCompoundDrawables(Left_Drawable、Top_Drawable、Right_Drawable、Bottom_Drawable) 要从您可以使用的资源中获取可绘制对象: getResources().getDrawable(R.drawable.your_drawable_id); Kotlin 扩展 + 可绘制周围的一些填充 fun TextView.addLeftDrawable(drawable: Int, padding: Int = 32) { val imgDrawable = ContextCompat.getDrawable(context, drawable) compoundDrawablePadding = padding setCompoundDrawablesWithIntrinsicBounds(imgDrawable, null, null, null) } 有两种方法可以做到这一点,您可以使用 XML 或 Java。 如果它是静态的并且不需要更改,那么您可以在 XML 中进行初始化。 android:drawableLeft="@drawable/cloud_up" android:drawablePadding="5sp" 现在如果您需要动态更改图标,那么您可以通过 根据事件调用图标 textViewContext.setText("File Uploaded"); textViewContext.setCompoundDrawablesWithIntrinsicBounds(R.drawable.uploaded, 0, 0, 0); 适用于我更改文本视图左/右可绘制颜色 for (drawable in binding.tvBloodPressure.compoundDrawablesRelative) { if (drawable != null) { drawable.colorFilter = PorterDuffColorFilter( ContextCompat.getColor(binding.tvBloodPressure.context, color), PorterDuff.Mode.SRC_IN ) } } 我就是这样用的。 txtUserName.setCompoundDrawablesWithIntrinsicBounds( requireActivity().getDrawable( R.drawable.ic_my_account ), null, null, null ) 上、下、左、右为图标资源。 TextView textView = (TextView) findViewById(R.id.your_id); textView.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom); // for example personalTextView = (TextView) findViewById(R.id.personal_info); personalTextView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.personal, 0, 0, 0); 在 Android 中,您可以使用 setCompoundDrawablesRelativeWithIntrinsicBounds 方法以编程方式设置 TextView 的左侧可绘制对象(也称为复合可绘制对象)。这是 Java 中的示例: import android.graphics.drawable.Drawable; import android.os.Bundle; import android.widget.TextView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Assume you have a TextView with the id "textView" TextView textView = findViewById(R.id.textView); // Set the left drawable programmatically Drawable drawable = getResources().getDrawable(R.drawable.your_drawable); // Replace with your drawable resource setLeftDrawable(textView, drawable); } private void setLeftDrawable(TextView textView, Drawable drawable) { // Set the left drawable with intrinsic bounds textView.setCompoundDrawablesRelativeWithIntrinsicBounds(drawable, null, null, null); } } 将 R.drawable.your_drawable 替换为您的可绘制对象的实际资源 ID。此示例假设您的布局 XML 文件中有一个 ID 为 textView 的 TextView。 请记住处理可绘制对象或 TextView 可能为 null 的情况,具体取决于您的应用程序逻辑。 static private Drawable **scaleDrawable**(Drawable drawable, int width, int height) { int wi = drawable.getIntrinsicWidth(); int hi = drawable.getIntrinsicHeight(); int dimDiff = Math.abs(wi - width) - Math.abs(hi - height); float scale = (dimDiff > 0) ? width / (float)wi : height / (float)hi; Rect bounds = new Rect(0, 0, (int)(scale * wi), (int)(scale * hi)); drawable.setBounds(bounds); return drawable; }

回答 11 投票 0

如何在 Android XML 中为文本添加下划线?

如何在 XML 文件中的文本下划线?我在 textStyle 中找不到选项。

回答 12 投票 0

如何将点击手势添加到串联 SwiftUI 文本视图的一部分?

我正在开发一个 SwiftUI 视图,我需要连接两个文本视图,并仅使连接文本的一部分可点击。这是一个简化的示例。 文本(“你好,”)+ 文本(“

回答 1 投票 0

在textView的左侧对齐一个字符串,在右侧对齐第二个字符串

所以我在 TextView 中正确对齐字符串时遇到了一些问题。我有 2 个字符串(左和右)和具有 match_parent 宽度的 TextView。关键是我找到的每个解决方案都...

回答 1 投票 0

Android:在文本视图周围添加边框

如何使用 xml 布局在文本周围添加边框,如图所示 我尝试在布局中添加边框,但它与文本重叠。

回答 7 投票 0

有没有办法在 GTK 窗口中设置 TextView 小部件的尺寸,该窗口已经包含一些使用固定容器的其他小部件?

我正在编写一个 GTK-3 应用程序,其中有一些按钮和一个 TreeView 模型。我已使用固定容器将所有这些小部件添加到主 GTK 窗口中。现在我想要一个固定的门廊...

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.