2가지 문제가 있어 어떻게 고쳐야 되는지 막막합니다...
지금은 컴파일 다 잘되고, 실행까진 가능한데, 저 Convert 버튼 누르면 에러납니다...
그리고 After왼쪽에 있는 박스에, forResult(단위 변환후) 값을 널려고 하는데 어떻게 해야하는지요...
forResult 옆에있는 " " 지우면 컴파일에 에러납니다...
val1 에다가 BeforeBox의 값을 저장할려고 하는데, 저 val1 = (R.id.AfterBox)로는 주소값이 저장이 되니
무슨 코드를 선언해야 되는지요.... 초보라서...ㅎㅎ;;
도와주세요.... ^^;
레이아웃 (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:gravity="left|right"
android:orientation="vertical"
android:background="#66FFFF" >
<FrameLayout
android:id="@+id/frameLayout2"
android:layout_width="match_parent"
android:layout_height="20dp" >
</FrameLayout>
<TextView
android:id="@+id/UnitText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/UnitText"
android:textColor="@android:color/black"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Spinner
android:id="@+id/Spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:prompt="@string/UnitPrompt"/>
<FrameLayout
android:id="@+id/frameLayout3"
android:layout_width="match_parent"
android:layout_height="20dp" >
</FrameLayout>
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="vertical" >
<Button
android:onClick="converter"
android:id="@+id/ConvertButton"
android:layout_width="131dp"
android:layout_height="wrap_content"
android:text="@string/ConvertText" />
</LinearLayout>
<FrameLayout
android:id="@+id/frameLayout1"
android:layout_width="match_parent"
android:layout_height="30dip" >
</FrameLayout>
<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/BeforeBox"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:inputType="numberDecimal" />
<TextView
android:id="@+id/BeforeText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/BeforeText"
android:textColor="@android:color/black"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/AfterBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal" />
<TextView
android:id="@+id/AfterText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/AfterText"
android:textColor="@android:color/black"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
</TableLayout>
</LinearLayout>
엑티비티 (....Activity.java) 파일은...
package arirang.unit.converter;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Button;
import android.widget.TextView;
public class BasicUnitConverterActivity extends Activity {
private double val1;
Spinner Type;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner1 = (Spinner) findViewById(R.id.Spinner1);
ArrayAdapter adapter1 = ArrayAdapter.createFromResource(
this, R.array.UnitList, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
}
public void converter() {
Type = (Spinner)findViewById(R.id.Spinner1);
val1 = (R.id.BeforeBox);
double dbResult = 0;
if(Type.getSelectedItem().toString().equals("meter to inch"))
{
dbResult = val1 * 39.3700787 ;
}
else if(Type.getSelectedItem().toString().equals("meter to feet"))
{
dbResult = val1 * 3.2808399;
}
else if(Type.getSelectedItem().toString().equals("feet to inch"))
{
dbResult = val1 * 12;
}
else if(Type.getSelectedItem().toString().equals("feet to meter"))
{
dbResult = val1 / 3.2808399;
}
else if(Type.getSelectedItem().toString().equals("inch to meter"))
{
dbResult = val1 / 39.3700787 ;
}
else if(Type.getSelectedItem().toString().equals("inch to feet"))
{
dbResult = val1 / 12;
}
else if(Type.getSelectedItem().toString().equals("kilogram to pound"))
{
dbResult = val1 * 2.20462262;
}
else if(Type.getSelectedItem().toString().equals("pound to kilogram"))
{
dbResult = val1 / 2.20462262;
}
else if(Type.getSelectedItem().toString().equals("Fahernheit to Celsius"))
{
dbResult=(val1 - 32) / (5 / 9);
}
else if(Type.getSelectedItem().toString().equals("Celsius to Fahernheit"))
{
dbResult = (9 / 5) * val1 + 32;
}
TextView ShowBox = (TextView)findViewById(R.id.AfterBox);
ShowBox.setText("forResult");
}
}
이렇게 코딩하였습니다....
조금 급한데 부탁합니다....^^
EditText etBefore = (EditText) findViewById(R.id.BeforeBox);
String beforeText = etBefore.getText().toString();
이런식이 View를 매칭시킨후 getText메소드를 통해 editText에 입력된 값을 이용하시면 됩니다.
double형 에 넣으려고 하시니 Double.valueOf 를 이용하셔서 변환하시면 되겠네요