鸿蒙next开发:UI开发-input开发指南

往期鸿蒙全套实战文章必看:(附带鸿蒙全栈学习资料)


input开发指南

input是交互式组件,用于接收用户数据。其类型可设置为日期、多选框和按钮等。

创建input组件

在pages/index目录下的hml文件中创建一个input组件。

<!-- xxx.hml -->
<div class="container">       
  <input type="text">             
     Please enter the content  
  </input>
</div>
/* xxx.css */
.container {
  width: 100%;
  height: 100%;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background-color: #F1F3F5;
}

设置input类型

通过设置type属性来定义input类型,如将input设置为button、date等。

<!-- xxx.hml -->
<div class="container">
  <div class="div-button">
    <dialog class="dialogClass" id="dialogId">
      <div class="content">
        <text>this is a dialog</text>
      </div>
    </dialog>
    <input class="button" type="button" value="click" onclick="btnclick"></input>
  </div>
  <div class="content">
    <input onchange="checkboxOnChange" checked="true" type="checkbox"></input>
  </div>
  <div class="content">
    <input type="date" class="flex" placeholder="Enter data"></input>
  </div>
</div>
/* xxx.css */
.container {
  width: 100%;
  height: 100%;
  align-items: center;
  flex-direction: column;
  justify-content: center;
  background-color: #F1F3F5 ;
}
.div-button {
  flex-direction: column;
  align-items: center;
}
.dialogClass{
  width:80%;
  height: 200px;
}
.button {
  margin-top: 30px;
  width: 50%;
}
.content{
  width: 90%;
  height: 150px;
  align-items: center;
  justify-content: center;
}
.flex {
  width: 80%;
  margin-bottom:40px;
}
// xxx.js
export default {
  btnclick(){
    this.$element('dialogId').show()
  },
}

说明

仅当input类型为checkbox和radio时,当前组件选中的属性是checked才生效,默认值为false。

事件绑定

向input组件添加search和translate事件。

<!-- xxx.hml -->
<div class="content">
  <text style="margin-left: -7px;">
    <span>Enter text and then touch and hold what you've entered</span>
  </text>
  <input class="input" type="text" onsearch="search" placeholder="search"> </input>
  <input class="input" type="text" ontranslate="translate" placeholder="translate"> </input>
</div>
/* xxx.css */
.content {
  width: 100%;
  height: 100%;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background-color: #F1F3F5;
}
.input {
  margin-top: 50px;
  width: 60%;
  placeholder-color: gray;
}
text{
  width:100%;
  font-size:25px;
  text-align:center;
}
// xxx.js
import promptAction from '@ohos.promptAction'
export default {
  search(e){
    promptAction.showToast({
      message:  e.value,
      duration: 3000,
    });
  },
  translate(e){
    promptAction.showToast({
      message:  e.value,
      duration: 3000,
    });
  }
}

设置输入提示

通过对input组件添加showError方法来提示输入的错误原因。

<!-- xxx.hml -->
<div class="content">
  <input id="input" class="input" type="text"  maxlength="20" placeholder="Please input text" onchange="change">
  </input>
  <input class="button" type="button" value="Submit" onclick="buttonClick"></input>
</div>
/* xxx.css */
.content {
  width: 100%;
  height: 100%;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background-color: #F1F3F5;
}
.input {
  width: 80%;
  placeholder-color: gray;
}
.button {
  width: 30%;
  margin-top: 50px;
}
// xxx.js
import promptAction from '@ohos.promptAction' 
 export default { 
   data:{ 
     value:'', 
   }, 
   change(e){ 
     this.value = e.value; 
     promptAction.showToast({ 
     message: "value: " + this.value, 
       duration: 3000, 
      }); 
   }, 
   buttonClick(e){ 
     if(this.value.length > 6){ 
       this.$element("input").showError({        
         error:  'Up to 6 characters are allowed.'       
       }); 
      }else if(this.value.length == 0){ 
        this.$element("input").showError({         
          error:this.value + 'This field cannot be left empty.'       
        }); 
      }else{ 
        promptAction.showToast({ 
          message: "success " 
        }); 
      } 
   }, 
 }

说明

该方法在input类型为text、email、date、time、number和password时生效。

场景示例

根据场景选择不同类型的input输入框,完成信息录入。

<!-- xxx.hml -->
<div class="container">    
  <div class="label-item"> 
    <label>memorandum</label>   
  </div>    
  <div class="label-item">        
    <label class="lab" target="input1">content:</label>        
    <input class="flex" id="input1" placeholder="Enter content" />    
  </div>    
  <div class="label-item">        
    <label class="lab" target="input3">date:</label>        
    <input class="flex" id="input3" type="date" placeholder="Enter data" />    
  </div>    
  <div class="label-item">        
    <label class="lab" target="input4">time:</label>        
    <input class="flex" id="input4" type="time" placeholder="Enter time" />    
  </div>   
  <div class="label-item">        
    <label class="lab" target="checkbox1">Complete:</label>        
    <input class="flex" type="checkbox" id="checkbox1" style="width: 100px;height: 100px;" />    
  </div>    
  <div class="label-item">        
    <input class="flex" type="button" id="button" value="save" onclick="btnclick"/>    
  </div>
</div>
/* xxx.css */
.container { 
  flex-direction: column;
  background-color: #F1F3F5;
}
.label-item {   
  align-items: center;
  border-bottom-width: 1px;border-color: #dddddd;
}
.lab {    
  width: 400px;}
label {    
  padding: 30px;
  font-size: 30px;      
  width: 320px;
  font-family: serif;
  color: #9370d8;
  font-weight: bold;
}
.flex {    
  flex: 1;
}
.textareaPadding {    
  padding-left: 100px;
}
// xxx.js
import promptAction from '@ohos.promptAction';
export default {    
  data: {    
  },    
  onInit() { 
  },   
  btnclick(e) {        
    promptAction.showToast({            
      message:'Saved successfully!'        
    })    
  }
}     

### 关于HarmonyOS 示例代码与开发教程 #### HarmonyOS NEXT 基础知识与开发教程 HarmonyOS NEXT华为推出的新一代操作系统,其开发涉及多个领域和技术栈。对于初学者来说,《鸿蒙之光HarmonyOS NEXT原生应用开发入门》是一本非常全面的学习材料[^1]。该书不仅介绍了环境搭建、ArkTS语言基础等内容,还提供了丰富的实践案例,例如 UI 开发、事件处理以及数据管理等。 以下是几个常见的开发场景及其对应的示例代码: --- #### 1. **省市区弹窗选择器** 在实际项目中,省市区的选择是一个常见需求。通过自定义组件可以轻松实现这一功能。以下是一个简单的 ArkTS 实现方案: ```typescript // 省市区选择器逻辑部分 @Entry @Component struct ProvinceCitySelector { @State selectedProvince: string = '请选择省份'; @State selectedCity: string = '请选择城市'; build() { Column() { Text('当前选中的地区:') .fontSize(20) Row() { Button(this.selectedProvince).onClick(() => { // 打开省份选择对话框 }) Button(this.selectedCity).onClick(() => { // 打开城市选择对话框 }) } }.padding(20) } } ``` 此代码片段展示了如何创建一个基本的交互界面来显示已选择的省份和城市,并提供按钮触发进一步操作[^2]。 --- #### 2. **验证码输入组件** 验证码输入是登录注册页面的重要组成部分。下面展示了一个基于 ArkUI 的简单验证码输入框设计: ```typescript // 验证码输入组件 @Entry @Component struct VerificationCodeInput { @State codeValue: string = ''; build() { Column() { TextField('', (value) => { this.codeValue = value; }) .inputType(InputType.Number) .placeholder('请输入验证码') .width('80%') Button('提交').onClick(() => { console.log(`用户输入的验证码为:${this.codeValue}`); }) }.space(16).alignItems(HorizontalAlign.Center) } } ``` 上述代码实现了带有占位符提示的文本框用于接收用户的验证码输入,并绑定到 `codeValue` 变量以便后续验证。 --- #### 3. **底部标签栏 TabBar** 为了提升用户体验,在许多应用程序中都会使用到底部导航栏作为主要入口之一。这里给出一种实现方法: ```typescript // 底部标签栏TabBar @Entry @Component struct BottomTabBarExample { @State currentPageIndex: number = 0; build() { Stack() { RouterView() Flex({ direction: FlexDirection.Row }) { Repeat(4, (_, index) => { Button(index === this.currentPageIndex ? 'Active' : '') .onClick(() => { this.currentPageIndex = index }); }).forEach(item => item.width('25%')) }.height(50).backgroundColor('#f0f0f0') } } } ``` 这段代码构建了一个具有四个选项卡的基本底部导航栏结构。 --- #### 图标资源获取途径 除了官方提供的 harmonyos-symbol 图标库外,还可以利用第三方平台上的矢量图形资源,比如阿里云 Iconfont 或 Bootstrap Icons 来丰富项目的视觉效果[^3]。 --- #### 学习资料推荐 如果希望获得更系统的指导,则可以通过访问《鸿蒙基础入门学习指南》,其中包含了大量针对不同层次读者准备的教学视频、文档及电子书籍等宝贵资源[^4]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值