安卓计算器 - EditView中无法输入小数 [英] Android Calculator - Editview cannot input decimal places

查看:143
本文介绍了安卓计算器 - EditView中无法输入小数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Andr​​oid code ++开发......我开发一个Android的计算器应用程序和不理解为什么两个EditTexts(第一输入和第二输入)不能接受小数,但只能输入整数...在这里,连接如下是codeS:

谢谢!

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

 包com.trial.jm4_calculator;

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

公共类MainActivity延伸活动{

    私人TextView的输出;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

    按钮BTN1 =(按钮)findViewById(R.id.button1);
    btn1.setOnClickListener(btn1Listener);
    输出=(TextView中)findViewById(R.id.lblOutput);
    }

View.OnClickListener btn1Listener =新View.OnClickListener(){
    公共无效的onClick(视图v){
       双opd1,OPD2;
       双重结果= 0.0;
       的EditText txtOpd1,txtOpd2;
       单选rdbAdd,rdbSubtract,rdbMultiply,rdbDivide;
       复选框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 =(单选)findViewById(R.id.rdbAdd);
       如果(rdbAdd.isChecked()){
           结果= opd1 + OPD2;
       }
       rdbSubtract =(单选)findViewById(R.id.rdbSubtract);
       如果(rdbSubtract.isChecked()){
           结果= opd1  -  OPD2;
       }
       rdbMultiply =(单选)findViewById(R.id.rdbMultiply);
       如果(rdbMultiply.isChecked()){
           结果= opd1 * OPD2;
       }
       rdbDivide =(单选)findViewById(R.id.rdbDivide);
       如果(rdbDivide.isChecked()){

              结果= opd1 / OPD2;
       }
       output.setText(答案=+结果);
    }
};
}
 

==================== main.xml中======================== ===========

 < XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
    机器人:layout_height =FILL_PARENT
    机器人:layout_width =FILL_PARENT
    机器人:方向=垂直
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <的LinearLayout
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_width =FILL_PARENT
        机器人:方向=横向>
        <的TextView
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_width =WRAP_CONTENT
            机器人:文本=首先输入:/>
        <的EditText
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_width =FILL_PARENT
            机器人:inputType =数字
            机器人:ID =@ + ID / txtOpd1/>
    < / LinearLayout中>
    < RadioGroup中
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_width =FILL_PARENT
        机器人:方向=横向
        机器人:ID =@ + ID / rdgOp>
        <单选按钮
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_width =WRAP_CONTENT
            机器人:文本=+
            机器人:ID =@ + ID / rdbAdd/>
        <单选按钮
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_width =WRAP_CONTENT
            机器人:文本= - 
            机器人:ID =@ + ID / rdbSubtract/>
        <单选按钮
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_width =WRAP_CONTENT
            机器人:文本=*
            机器人:ID =@ + ID / rdbMultiply/>
        <单选按钮
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_width =WRAP_CONTENT
            机器人:文本=/
            机器人:ID =@ + ID / rdbDivide/>
    < / RadioGroup中>
    <的LinearLayout
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_width =FILL_PARENT
        机器人:方向=横向>
        <的TextView
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_width =WRAP_CONTENT
            机器人:文本=第二个输入:/>
            <的EditText
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_width =FILL_PARENT
                机器人:inputType =数字
                机器人:ID =@ + ID / txtOpd2/>
     < / LinearLayout中>
     <按钮
         机器人:layout_height =WRAP_CONTENT
         机器人:layout_width =WRAP_CONTENT
         机器人:文本=计算
         机器人:ID =@ + ID /按钮1/>
     <的TextView
         机器人:layout_height =WRAP_CONTENT
         机器人:layout_width =WRAP_CONTENT
         机器人:ID =@ + ID / lblOutput/>
< / LinearLayout中>
 

解决方案

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

使用XML属性安卓inputType =numberDecimal在你的EditText小部件您的EditText宣言将是这样的:

 <的EditText
    机器人:ID =@ + ID / editText1
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:EMS =10
    机器人:inputType =numberDecimal/>
 

如果你想使用符号十进制数比将二者结合起来的XML属性安卓inputType =numberDecimal Android的:inputType =numberSigned。您的EditText宣言将是这样的:

 <的EditText
    机器人:ID =@ + ID / editText1
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:EMS =10
    机器人:inputType =numberDecimal | numberSigned>

< /的EditText>
 

I am new to Android code development...I am developing a Android calculator apps and does not understand why the two EditTexts (first input and second input) cannot accept decimal places but can only input integers...Here attached as follows are the codes:

Thanks!

=============Main Activity===============================

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>

解决方案

If you want to use Decimal Number only on your EditText

use the xml attribute android:inputType="numberDecimal" in your EditText widget your EditText declaration will be like this:

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

If you want to use Signed Decimal Number than combine the two Xml attributes android:inputType="numberDecimal" and android:inputType="numberSigned". Your EditText declaration will be like this:

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

</EditText>

这篇关于安卓计算器 - EditView中无法输入小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆