通常輸入數值都會使用EditText等等的文字框當作對象, 如何才能很快速的從這些view取得數值呢??
使用 getText().toString() 加上各個數值類別(Integer, Double, Float, Long, Byte, Short)的ParseXXX() 最快 如下
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class GetValueFromTextView extends Activity { private static final int PERCENT = 100; private static final int MONTH_COUNT = 12; private EditText mInput_lend; private EditText mInputRate; private EditText mInputNumber; private Button mSubmit; private TextView mAmount; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initComponents(); } private void initComponents() { mInput_lend = (EditText) findViewById(R.id.input_lend); mInputNumber = (EditText) findViewById(R.id.input_number); mInputRate = (EditText) findViewById(R.id.input_rate); mSubmit = (Button) findViewById(R.id.submit); mSubmit.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mAmount.setText("" + countAmount()); } }); mAmount = (TextView) findViewById(R.id.amount); } private double countAmount() { int lend = Integer.parseInt(mInput_lend.getText().toString()); double rateOfMonth = Double.parseDouble(mInputRate.getText().toString()) / MONTH_COUNT / PERCENT; int number = Integer.parseInt(mInputNumber.getText().toString()); return lend * (Math.pow(rateOfMonth + 1, number)); } }
重點在 countAmount 方法中, 以下列方式取得string並轉換成數字
int lend = Integer.parseInt(mInput_lend.getText().toString()); double rateOfMonth = Double.parseDouble(mInputRate.getText().toString()) / MONTH_COUNT / PERCENT; int number = Integer.parseInt(mInputNumber.getText().toString());
但要小心轉型溢位的問題
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/main_tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/title"/> <TextView android:id="@+id/main_tv_lendHint" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/lend"/> <EditText android:id="@+id/input_lend" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="number"/> <TextView android:id="@+id/main_tv_rateHint" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/rate"/> <EditText android:id="@+id/input_rate" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="numberDecimal"/> <TextView android:id="@+id/main_tv_number" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/rate"/> <EditText android:id="@+id/input_number" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="number"/> <Button android:id="@+id/submit" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/btn"/> <TextView android:id="@+id/amount" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
以及 strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Count Round-amount savings</string> <string name="title">Round-amount savings:</string> <string name="lend">capital(NT$):</string> <string name="rate">rate of year(%):</string> <string name="number">number of periods:</string> <string name="btn">count</string> </resources>