Android计算器-Editview无法输入小数位

2024-03-12

我是Android代码开发新手...我正在开发一个Android计算器应用程序,不明白为什么两个EditText(第一个输入和第二个输入)不能接受小数位而只能输入整数...附如下是代码:

Thanks!

============主要活动================================

package com.trial.jm4_calculator;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.support.v4.app.NavUtils;

public class MainActivity extends Activity {

    private TextView output;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    Button btn1 = (Button) findViewById(R.id.button1);
    btn1.setOnClickListener(btn1Listener);
    output = (TextView) findViewById(R.id.lblOutput);
    }

View.OnClickListener btn1Listener = new View.OnClickListener() {
    public void onClick(View v) {
       double opd1, opd2;
       double result = 0.0;
       EditText txtOpd1, txtOpd2;
       RadioButton rdbAdd, rdbSubtract, rdbMultiply, rdbDivide;
       CheckBox chkDivide;

       txtOpd1 = (EditText) findViewById(R.id.txtOpd1);
       txtOpd2 = (EditText) findViewById(R.id.txtOpd2);

       opd1 = Double.parseDouble(txtOpd1.getText().toString());
       opd2 = Double.parseDouble(txtOpd2.getText().toString());

       rdbAdd = (RadioButton) findViewById(R.id.rdbAdd);
       if (rdbAdd.isChecked()) {
           result = opd1 + opd2;
       }
       rdbSubtract = (RadioButton) findViewById(R.id.rdbSubtract);
       if (rdbSubtract.isChecked()) {
           result = opd1 - opd2;
       }
       rdbMultiply = (RadioButton) findViewById(R.id.rdbMultiply);
       if (rdbMultiply.isChecked()) {
           result = opd1 * opd2;
       }
       rdbDivide = (RadioButton) findViewById(R.id.rdbDivide);
       if (rdbDivide.isChecked()) {

              result = opd1 / opd2;            
       }
       output.setText("Answer = " + result);
    }
};
}

===================Main.xml============================ ========

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent" 
    android:orientation="vertical" 
    xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout 
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent" 
        android:orientation="horizontal"> 
        <TextView 
            android:layout_height="wrap_content" 
            android:layout_width="wrap_content" 
            android:text="First Input: "/> 
        <EditText 
            android:layout_height="wrap_content" 
            android:layout_width="fill_parent" 
            android:inputType="number" 
            android:id="@+id/txtOpd1"/> 
    </LinearLayout>
    <RadioGroup 
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent" 
        android:orientation="horizontal" 
        android:id="@+id/rdgOp"> 
        <RadioButton 
            android:layout_height="wrap_content" 
            android:layout_width="wrap_content" 
            android:text="+ " 
            android:id="@+id/rdbAdd"/> 
        <RadioButton 
            android:layout_height="wrap_content" 
            android:layout_width="wrap_content" 
            android:text="- " 
            android:id="@+id/rdbSubtract"/> 
        <RadioButton 
            android:layout_height="wrap_content" 
            android:layout_width="wrap_content" 
            android:text="* " 
            android:id="@+id/rdbMultiply"/> 
        <RadioButton 
            android:layout_height="wrap_content" 
            android:layout_width="wrap_content" 
            android:text="/ " 
            android:id="@+id/rdbDivide"/> 
    </RadioGroup>
    <LinearLayout 
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent" 
        android:orientation="horizontal"> 
        <TextView 
            android:layout_height="wrap_content" 
            android:layout_width="wrap_content" 
            android:text="Second Input: "/> 
            <EditText 
                android:layout_height="wrap_content" 
                android:layout_width="fill_parent" 
                android:inputType="number" 
                android:id="@+id/txtOpd2"/> 
     </LinearLayout> 
     <Button 
         android:layout_height="wrap_content" 
         android:layout_width="wrap_content" 
         android:text="Compute" 
         android:id="@+id/button1"/> 
     <TextView 
         android:layout_height="wrap_content" 
         android:layout_width="wrap_content" 
         android:id="@+id/lblOutput"/> 
</LinearLayout>

如果你想使用十进制数仅在您的 EditText 上

使用 xml 属性android:inputType="numberDecimal"在您的 EditText 小部件中,您的 EditText 声明将如下所示:

<EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="numberDecimal" />

如果你想使用有符号十进制数而不是组合两个 Xml 属性android:inputType="numberDecimal" and android:inputType="numberSigned"。您的 EditText 声明将如下所示:

<EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="numberDecimal|numberSigned" >

</EditText>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android计算器-Editview无法输入小数位 的相关文章

随机推荐

  • 如何将 std::array 转换为 std::tuple? [复制]

    这个问题在这里已经有答案了 我正在开发一个辅助模块来在多态对象之间传递值 并且在某些时候我有 std array
  • 如何通过相应文档的数组获取每个组的字段最大值?

    我有一个像这样的集合 id ObjectId 5738cb363bb56eb8f76c2ba8 records Name Joe Salary 70000 Department IT id ObjectId 5738cb363bb56eb8
  • Firefox 扩展如何以编程方式获取自己的版本号?

    如何使用 Javascript 以编程方式获取我自己的 Firefox 扩展版本号 我的扩展有一个 install rdf 文件 其中包含类似于下面的版本号 我想提取其中的内容
  • CMYK颜色直接转HSV颜色

    如何将HSV颜色直接转换为CMYK颜色 提及做到这一点的 JavaScript 库会加分 我只见过将 HSV 转换为 RGB 然后将 RGB 转换为 CMYK 的解决方案 我知道的唯一解决方案是转换为 RGB 作为中间层 然后将其转换为您想
  • 配置 WCF 服务以接受不合格参数

    我的 WCF 服务无法识别以不合格形式发送的请求参数值 而是替换为默认值 例如 此请求将产生 您输入 21 的结果
  • 如何重新配置​​ Meteor 的 Facebook 账户,或者 Meteor 的 Facebook 配置在哪里?

    流星的accounts facebook包的设置非常容易 为了输入 Facebook 应用程序 ID 和秘密令牌 我在浏览器中加载了 Meteor Web 应用程序 单击登录按钮 然后单击 配置 Facebook 它要求我提供应用程序 ID
  • C# ASP.NET 列表视图

    我正在尝试创建一个包含多个产品列表的视图 下面的示例展示了产品列表的外观 我不确定是否应该使用表格并为每个新产品或其他内容创建一个新表格 我不是一个很好的 ASP NET 开发人员 我不知道如何解决这个问题 基本上 如果我有 10 个结果
  • JS中的罗马到整数为什么只转换第一个字符

    我尝试解决Leedcode问题13 问题是给定一个罗马数字 将其转换为整数 输入保证在1到3999的范围内 这是我的代码 我想知道为什么它只将罗马数字中的第一个字符转换为整数 var romanToInt function s var re
  • 使用 mod_rewrite 将所有流量重定向到 index.php

    我正在尝试构建一个 url 缩短器 并且我希望能够在域之后立即获取任何字符并将它们作为变量 url 传递 例如 http google com asdf http google com asdf 会成为 http www google co
  • 从数据库中选择最受欢迎的地点的算法

    我们有一个包含地点数据库的网站 对于每个地方 我们的用户都可以执行我们记录的以下操作之一 查看 查看其个人资料 评级 按 1 5 星的等级对其进行评级 审查 审查它 已完成 标记他们已经去过那里 愿望清单 标记他们想去那里 最喜欢的 标记这
  • 实体框架,更新前值的触发机制

    EF 中是否有在更新对象值之前具有的内容 例如当实体对象 假设用户 被保存时 我想在更新用户对象值之前了解日志记录的目的 Thanks 如果您与ObjectContext edmx 您可以订阅SavingChanges http msdn
  • 为什么我无法实例化 switch-case 块内的对象

    我的代码有 3 个类 n hexa n octa n bin 代码在这里 switch choice case 1 cin gt gt n n hexa nx n break case 2 cin gt gt n n octa no n b
  • 如何使用 gulp 在浏览器中进行刷新

    我有一个应用程序在 iis 中 它是一个用 angularjs 和 webapi C 2 0 制作的应用程序 我想创建一个任务 在保存任何 js 文件后立即更新浏览器 吞咽版本 3 9 1 gulp task livereload func
  • 尝试在本地环境中调试 LeetCode 答案时出错

    我正在研究 LeetCode 问题199 二叉树右侧视图 https leetcode com problems binary tree right side view 给定二叉树的根 想象自己站在它的右侧 返回您可以看到从上到下排序的节点
  • 无法使用通过 VS2010 发布在 Windows Azure 上发布网站

    当我尝试通过 Visual Studio 2010 在 Windows Azure 上发布我的 Web 应用程序时 我遇到了这个问题 我尝试右键单击项目并选择发布并导入从 Windows Azure 平台下载的发布配置文件 我收到此错误 错
  • 加入多个文件

    我正在使用标准连接命令来连接两个基于column1 的排序文件 命令很简单 join file1 file2 gt output file 但是如何使用相同的技术连接 3 个或更多文件呢 连接 file1 file2 file3 gt 输出
  • 在运行时根据输入从属性文件中获取值 - java Spring

    我的 color properties 文件为 rose red lily white jasmine pink 我需要获取颜色值 String flower runTimeFlower Value flower String colour
  • Playframework 2.0.x 是否支持模板中的

    不知道play 2 0 3及更高版本是否支持else if在视图中 我只读到必须这样编码 if else if else 不敢相信 我在第二个 if 之前使用了 if true else if true else
  • 使用 nltk 没有上下文的词性标记

    有没有一种简单的方法来确定给定单词最可能的词性标签没有上下文使用nltk 或者如果不使用任何其他工具 数据集 我尝试使用 wordnet 但似乎 sysnet 不是按可能性排序的 gt gt gt wn synsets says Synse
  • Android计算器-Editview无法输入小数位

    我是Android代码开发新手 我正在开发一个Android计算器应用程序 不明白为什么两个EditText 第一个输入和第二个输入 不能接受小数位而只能输入整数 附如下是代码 Thanks 主要活动 package com trial j