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

Android计算器-Editview无法输入小数位,第1张

概述我是Android代码开发的新手…我正在开发一个Android计算器应用程序,不理解为什么两个EditText(第一个输入和第二个输入)不能接受小数位,而只能输入整数…下面是代码:谢谢!=============主要活动================================packagecom.trial.jm4_calculator;importandro

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

谢谢!

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

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上使用十进制数

在EditText小部件中使用xml属性androID:inputType =“ numberDecimal”,您的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”和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>
总结

以上是内存溢出为你收集整理的Android计算器-Editview无法输入小数位全部内容,希望文章能够帮你解决Android计算器-Editview无法输入小数位所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1093438.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存