通常,从EditText中获取字符串很简单:

    EditText text = findViewById(R.id.textName);
    String name = text.getText().toString();

现在要从其中获得数字,就要做以下处理。

xml中加入输入类型android:inputType="number",这样使得弹出的输入键盘为数字键盘。之前的android:numeric="integer"已经过时。

    <EditText
        android:id="@+id/editText_age"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="input age"
        android:inputType="number" />

Java文件中:

    EditText et_age = findViewById(R.id.editText_age);
    int age = Integer.parseInt(et_age.getText().toString());

有网友说直接使用getText()会报错,要使用getEditableText()。另外,trim()可以去掉字符串两端的的空格。

 
扩展

既然用到了android:inputType属性,就介绍一下常见的类型。

        android:inputType="none"                //输入普通字符
        android:inputType="text"                //输入普通字符
        android:inputType="textCapCharacters"   //所有字母大写
        android:inputType="textCapWords"        //单词首字母大写
        android:inputType="textCapSentences"    //仅第一个字母大写
        android:inputType="textAutoCorrect"     //自动完成
        android:inputType="textAutoComplete"    //自动完成
        android:inputType="textMultiLine"       //多行输入
        android:inputType="textImeMultiLine"    //输入法多行(不一定支持)
        android:inputType="textNoSuggestions"   //不提示
        android:inputType="textUri"             //URI格式
        android:inputType="textEmailAddress"    //电子邮件地址格式
        android:inputType="textEmailSubject"    //邮件主题格式
        android:inputType="textShortMessage"    //短消息格式
        android:inputType="textLongMessage"     //长消息格式
        android:inputType="textPersonName"      //人名格式
        android:inputType="textPostalAddress"   //邮政格式
        android:inputType="textPassword"        //密码格式
        android:inputType="textVisiblePassword" //密码可见格式
        android:inputType="textWebEditText"     //作为网页表单的文本格式
        android:inputType="textFilter"          //文本筛选格式
        android:inputType="textPhonetic"        //拼音输入格式
     
     
        //数值类型
        android:inputType="number"              //数字格式
        android:inputType="numberSigned"        //有符号数字格式
        android:inputType="numberDecimal"       //可以带小数点的浮点格式
        android:inputType="phone"               //拨号键盘
        android:inputType="datetime"            //日期+时间格式
        android:inputType="date"                //日期键盘
        android:inputType="time"                //时间键盘

原文链接:https://blog.csdn.net/qq_35413770/article/details/104781833

android input的更多相关文章

  1. 关于Appium android input manager for Unicode 提示信息

    Appium调完输入法后,会弹出  Appium android input manager for Unicode 提示信息相关的提示信息,每次运行如此,如下图 网络上查找一遍,基本解决了,只要在设 ...

  2. Linux/Android——Input系统之InputMapper 处理 (八)【转】

    本文转载自:http://blog.csdn.net/jscese/article/details/43561773 前文Linux/Android——Input系统之InputReader (七)介 ...

  3. Linux/Android——Input系统之InputReader (七)【转】

    本文转载自:http://blog.csdn.net/jscese/article/details/42739197 在前文Linux/Android——Input系统之frameworks层Inpu ...

  4. Linux/Android——Input系统之frameworks层InputManagerService (六)【转】

    本文转载自:http://blog.csdn.net/u013491946/article/details/72638954 版权声明:免责声明: 本人在此发文(包括但不限于汉字.拼音.拉丁字母)均为 ...

  5. Linux/Android——input系统之 kernel层 与 frameworks层交互 (五)【转】

    本文转载自:http://blog.csdn.net/jscese/article/details/42291149 之前的四篇博文记录的都是linux中的input体系相关的东西,最底层以我调试的u ...

  6. input系统——android input系统

    AndroidInput系统--JNI NativeInputManager InputManger InputReader AndroidInput系统--InputReader AndroidIn ...

  7. Linux/Android——input子系统核心 (三)【转】

    本文转载自:http://blog.csdn.net/jscese/article/details/42123673 之前的博客有涉及到linux的input子系统,这里学习记录一下input模块. ...

  8. Android input输入框 移动页面input手机键盘中的“搜索”按键

    动页面input手机键盘中的“搜索”按键 满足以下几点机即可: input type="search"    放到form标签中    使用action属性 <form ac ...

  9. Android Input设备debug技巧

    一.驱动层 检查是否有点上报 adb shell getevent -l /dev/input/eventX 检查input设备支持的属性值 adb shell getevent -i /dev/in ...

  10. Android入门(十):界面的布局方式及其实际应用

    关于Android界面布局,网上已经有了很多非常不错的学习资料,在这里我也不班门弄斧了,推荐两篇我认为写的不错的教程,然后再重点讲一下几种布局方式的实际应用. 教程链接:①http://www.cnb ...

随机推荐

  1. FastAPI复杂查询终极指南:告别if-else的现代化过滤架构

    title: FastAPI复杂查询终极指南:告别if-else的现代化过滤架构 date: 2025/3/14 updated: 2025/3/14 author: cmdragon excerpt ...

  2. vue3-webseek网页版AI问答|Vite6+DeepSeek+Arco流式ai聊天打字效果

    2025 AI实战vue3+deepseek+arcoDesign仿DeepSeek/豆包网页版AI聊天助手. vue3-web-deepseek 实战网页PC版智能AI对话,基于vite6+vue3 ...

  3. 网络编程-关闭连接(1)-C/C++相关系统调用

    背景 在linux网络编程中,经常需要编写关闭socket的代码,比如心跳检测失败需要关闭重连:网络报异常需要关闭重连.但究竟关闭操作做了什么,却不太清楚.目前项目使用Netty框架来实现的网络编程, ...

  4. 【FAQ】HarmonyOS SDK 闭源开放能力 —Push Kit(12)

    1.问题描述: pushdeviceid的长度是固定的吗? 解决方案: 在鸿蒙系统中,设备ID的长度是固定的. 2.问题描述: 通过REST API三方推送IM类消息,如何实现应用处于前台时不展示三方 ...

  5. Crealens.ai 免费体验GPT-4o 生图+吉卜力风格化

    自己的一张日常照片,能在几秒内变身为一幅充满童话感的吉卜力风插画?90%的人不知道,如今只需一句话,就能在 ChatGPT 里实现"AI 生图"--这就是 GPT-4o 生图功能的 ...

  6. go: no such tool "compile"(记录)

    这是一次离谱问题和胡搞一通莫名解决的记录 背景:win11系统下,原有的go1.18更新到go1.19后出现了莫名的go: no sucn tool "compile"的情况. 当 ...

  7. RL · Exploration | 使用时序距离构造 intrinsic reward,鼓励 agent 探索

    论文标题:Episodic Novelty Through Temporal Distance. ICLR 2025,8 8 6 5 poster. arxiv:https://arxiv.org/a ...

  8. Mybatis的动态SQL的语句

    例子. <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC &quo ...

  9. 多年后再做Web开发,AI帮大忙

    最近在AI辅助下做一个简单的网站开发,真是感叹AI的强大呀! 虽然多年前我做过Web前端和后端开发,但是那时候的开发没有现在这么容易入门.上手. 之前SSH(Structs2+Spring+Hiber ...

  10. require、include、require_once和use的区别/命名空间的使用方法

    1.require.include require.include都是文件包含,不同的是require语句会输出错误信息,并且立即终止脚本处理.而include语句在没有找到文件时则会输出警告,不会终 ...