我是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>