我剛剛得到了一個Droid,使用它一段時間後,我覺得我想爲它做一個程序。我正在嘗試製作的程序計算輔助存儲介質的實際存儲容量。用戶從KB到YB範圍內的單位列表中進行選擇,並根據所選單位將輸入的大小放入公式中。但是,該程序存在一些問題。從我的測試中,我已經縮小到事實,即用戶的選擇不是真的從微調獲得。我所看到的一切似乎都指向一種與J2SE中的工作方式非常類似的方法,但它什麼都不做。我實際上應該如何獲得這些數據?獲取用戶選擇並將其轉換爲字符串[Android]
下面是應用程序的Java源代碼:
package com.Actual.android;
import android.app.Activity;
import android.os.Bundle;
import android.widget.*;
import android.view.*;
public class ActualStorageActivity extends Activity
{
Spinner selection; /* declare variable, in order to control spinner (ComboBox) */
ArrayAdapter adapter; /* declare an array adapter object, in order for spinner to work */
EditText size; /* declare variable to control textfield */
EditText result; /* declare variable to control textfield */
Button calculate; /* declare variable to control button */
Storage capacity = new Storage(); /* import custom class for formulas */
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main); // load content from XML
selection = (Spinner)findViewById(R.id.spinner);
adapter = ArrayAdapter.createFromResource(this, R.array.choices_array, android.R.layout.simple_spinner_dropdown_item);
size = (EditText)findViewById(R.id.size);
result = (EditText)findViewById(R.id.result);
calculate = (Button)findViewById(R.id.submit);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); /* set resource for dropdown */
selection.setAdapter(adapter); // attach adapter to spinner
result.setEnabled(false); // make read-only
result.setText("usable storage");
}
public void calcAction(View view) {
String initial = size.getText().toString();
String unit = selection.getSelectedItem().toString();
String end = "Nothing";
double convert = Double.parseDouble(initial);
capacity.setStorage(convert);
if (unit == "KB")
{
end = Double.toString(capacity.getKB());
}
else if (unit == "MB")
{
end = Double.toString(capacity.getMB());
}
else if (unit == "GB")
{
end = Double.toString(capacity.getGB());
}
else if (unit == "TB")
{
end = Double.toString(capacity.getTB());
}
else if (unit == "PB")
{
end = Double.toString(capacity.getPB());
}
else if (unit == "EB")
{
end = Double.toString(capacity.getEB());
}
else if (unit == "ZB")
{
end = Double.toString(capacity.getZB());
}
else if (unit == "YB")
{
end = Double.toString(capacity.getYB());
}
else;
result.setText(end);
}
}
這是非常不可讀的,請確保您在帖子中突出顯示您的代碼並單擊代碼圖標。也儘量簡化你的問題,太久了! – 2010-05-05 19:32:00
你走了,好多了;) – 2010-05-05 20:09:46