You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Flutter中实现指定样式的禁用TextBox?

实现禁用状态的只读文本框

以下是不同技术场景下的实现方案:

HTML/前端场景

  • 方法1:使用disabled属性
    直接为单行/多行文本框添加disabled属性,控件会自动变为禁用样式,且完全无法交互编辑:
<!-- 单行文本框 -->
<input type="text" value="禁用状态的内容" disabled>

<!-- 多行文本框 -->
<textarea disabled>禁用状态的多行内容</textarea>
  • 方法2:使用readonly属性(需保留表单提交值时用)
    disabled控件不会随表单提交数据,若需要提交内容但禁止编辑,可使用readonly属性,再配合CSS模拟禁用样式:
<input type="text" value="可提交但无法编辑" readonly style="background-color: #eee; color: #666; border: 1px solid #ccc;">

WinForms场景

针对WinForms的TextBox控件,有两种常用方式:

  • 设置Enabled = false:直接禁用控件,自动应用系统灰色禁用样式,无法编辑和聚焦:
textBox1.Enabled = false;
  • 设置ReadOnly = true:控件可聚焦但不能编辑,可手动修改样式模拟禁用效果:
textBox1.ReadOnly = true;
textBox1.BackColor = SystemColors.Control;
textBox1.ForeColor = SystemColors.GrayText;

WPF场景

WPF中实现禁用只读文本框的方式:

  • 设置IsEnabled = false:禁用控件,应用系统默认禁用样式,完全无法交互:
<TextBox Text="禁用状态的内容" IsEnabled="False"/>
  • 设置IsReadOnly = true:只读状态可聚焦,配合样式修改外观模拟禁用效果:
<TextBox Text="只读但可聚焦" IsReadOnly="True" Background="#EEEEEE" Foreground="#666666"/>

内容的提问来源于stack exchange,提问作者Gayatri Dyunakhe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 02:25:28