android-studio - 在Android中输入完成后如何将多行编辑文本限制为单行?
问题描述
我正在使用多行 EditText。输入完成并触摸另一个视图后,我希望 editText 只出现一行。怎么做?
<EditText
android:id="@+id/descriptionfield"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
/>
解决方案
您可以设置setOnFocusChangeListener
为您的edittext,如下所示
descriptionfield.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
descriptionfield.setSingleLine(false);
} else {
descriptionfield.setSingleLine(true);
}
}
});
如果edittext有焦点然后setSingleLine
到false
else setSingleLine
totrue
我希望这可以帮助你
推荐阅读
- node.js - 空通用行参数
- excel - 使用 Python 在 xml 中的节点之前解析注释
- c# - 为什么我的 API POST 请求总是失败?
- python - 有没有办法在每次执行 for 循环时更改变量?
- javascript - 两个foreach替换JS
- mongodb - MongoDB 复制问题 - AWS 和 Windows 主机
- c# - C# Dynamic Linq:字符串被假定为整数?
- oracle - 通过 Attunity for SSIS 测试与 Oracle 连接器的连接时出现 ORACLE_HOME 错误
- php - 使用 codeigniter 从 5 升级到 PHP 7
- ios - 使用 SwiftyJSON 解析 JSON - 更新 UI 时遇到问题