问题
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>
回答1:
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>
回答2:
Change android:inputType from "number" to "numberDecimal". See the documentation for even more options for inputType.
回答3:
inputType="number" doesnt allow floats. try changing:
android:inputType="number"
to:
android:numeric="integer|decimal"
回答4:
You need to change the input type of your EditText in the XML code.
Change the inputType attribute of the EditText from
android:inputType="number"
to
android:inputType="numberDecimal"
<EditText
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:inputType="numberDecimal"
android:id="@+id/txtOpd1"/>
来源:https://stackoverflow.com/questions/12021068/android-calculator-editview-cannot-input-decimal-places