Android 如何处理EditText中的华氏温度并打印相应的摄氏温度?
我对在Android中创建监听器的概念感到困惑。我的程序的目的很简单:在单位之间转换(如华氏到摄氏,摄氏到华氏,等等)。如何获取EditText对象中的文本,然后如何将其打印出来?我每次运行时都会遇到一个错误(不幸的是,Converter.exe已经停止)。这是到目前为止我的代码。请告知Android 如何处理EditText中的华氏温度并打印相应的摄氏温度?,android,eclipse,textview,Android,Eclipse,Textview,我对在Android中创建监听器的概念感到困惑。我的程序的目的很简单:在单位之间转换(如华氏到摄氏,摄氏到华氏,等等)。如何获取EditText对象中的文本,然后如何将其打印出来?我每次运行时都会遇到一个错误(不幸的是,Converter.exe已经停止)。这是到目前为止我的代码。请告知 import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Bu
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class ConverterActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.temp_Button);
button.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
setContentView(R.layout.body);
}
});
Button ok = (Button) findViewById(R.id.OK_Button);
ok.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
EditText et = (EditText) getText(0);
final int num = Integer.parseInt(et.toString());
double answer = (num - 32) * 5/9;
TextView tv = (TextView) findViewById(R.id.editText);
tv.setText(Double.toString(answer));
}
});
}
}
我还有两个布局(.xml)文件(我不确定这是否是正确的编程风格)
main仅显示可单击的按钮。然后,在单击按钮后,出现第二个.xml文件
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" >
<Button
android:id="@+id/temp_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/F" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/C" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/I" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Cm" />
</LinearLayout>
其目的是从用户输入的EditText对象中获取数字,将其转换并打印出来,但我不确定我是否做对了,或者这是否在正确的位置
**更新:这里是日志
02-06 19:53:36.148: D/AndroidRuntime(529): Shutting down VM
02-06 19:53:36.158: W/dalvikvm(529): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
02-06 19:53:36.168: E/AndroidRuntime(529): FATAL EXCEPTION: main
02-06 19:53:36.168: E/AndroidRuntime(529): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sapra.converter/com.sapra.converter.ConverterActivity}: java.lang.NullPointerException
02-06 19:53:36.168: E/AndroidRuntime(529): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
02-06 19:53:36.168: E/AndroidRuntime(529): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
02-06 19:53:36.168: E/AndroidRuntime(529): at android.app.ActivityThread.access$600(ActivityThread.java:123)
02-06 19:53:36.168: E/AndroidRuntime(529): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
02-06 19:53:36.168: E/AndroidRuntime(529): at android.os.Handler.dispatchMessage(Handler.java:99)
02-06 19:53:36.168: E/AndroidRuntime(529): at android.os.Looper.loop(Looper.java:137)
02-06 19:53:36.168: E/AndroidRuntime(529): at android.app.ActivityThread.main(ActivityThread.java:4424)
02-06 19:53:36.168: E/AndroidRuntime(529): at java.lang.reflect.Method.invokeNative(Native Method)
02-06 19:53:36.168: E/AndroidRuntime(529): at java.lang.reflect.Method.invoke(Method.java:511)
02-06 19:53:36.168: E/AndroidRuntime(529): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-06 19:53:36.168: E/AndroidRuntime(529): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-06 19:53:36.168: E/AndroidRuntime(529): at dalvik.system.NativeStart.main(Native Method)
02-06 19:53:36.168: E/AndroidRuntime(529): Caused by: java.lang.NullPointerException
02-06 19:53:36.168: E/AndroidRuntime(529): at com.sapra.converter.ConverterActivity.onCreate(ConverterActivity.java:29)
02-06 19:53:36.168: E/AndroidRuntime(529): at android.app.Activity.performCreate(Activity.java:4465)
02-06 19:53:36.168: E/AndroidRuntime(529): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
02-06 19:53:36.168: E/AndroidRuntime(529): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
02-06 19:53:36.168: E/AndroidRuntime(529): ... 11 more
举个例子。我不久前写了这个温度转换器示例。也许有人会觉得它有用
这很简单,我想你可以从这里找到答案
// This is the TextView you set in your layout. It will display the temperature.
final TextView tv = (TextView) findViewById(R.id.tv);
// This sets up the EditText so you can capture the input and return the temperature.
final EditText edittext = (EditText) findViewById(R.id.et);
edittext.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN)
&& (keyCode == KeyEvent.KEYCODE_ENTER)) {
// Perform action on key press
float f = Float.parseFloat(edittext.getText().toString());
tv.setText(String.valueOf(convertCelciusToFahrenheit(f)));
return true;
}
return false;
}
});
}
// Converts to celcius
private float convertFahrenheitToCelcius(float fahrenheit) {
return ((fahrenheit - 32) * 5 / 9);
}
// Converts to fahrenheit
private float convertCelciusToFahrenheit(float celsius) {
return ((celsius * 9) / 5) + 32;
}
什么错误?而且
getText(0)
不正确。使用getText().toString()
。总之,我的问题是我的代码是否正确?我纠正了你提到的错误(谢谢你)。你看到什么特别的错误了吗?当你点击按钮时会发生什么?你有日志吗?这些都是需要提及和上传的有用内容。刚刚添加了日志。谢谢您的时间。ConverterActivity中的第29行是什么?哇,看起来很混乱。另外,我试着在我的计算机上运行它,它给了我一个f.format的编译器错误。我只是运行它没有任何问题。如果我不是一个工作样本,我不会发布它。你可能导入了错误的格式化程序。是的,可能是我的错。很抱歉但是你能帮我吗?非常感谢你的帮助。我真的很感激。我将从这里开始工作,看看我是否能更多地理解这个程序的语法。(或许还可以进一步扩展:))
02-06 19:53:36.148: D/AndroidRuntime(529): Shutting down VM
02-06 19:53:36.158: W/dalvikvm(529): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
02-06 19:53:36.168: E/AndroidRuntime(529): FATAL EXCEPTION: main
02-06 19:53:36.168: E/AndroidRuntime(529): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sapra.converter/com.sapra.converter.ConverterActivity}: java.lang.NullPointerException
02-06 19:53:36.168: E/AndroidRuntime(529): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
02-06 19:53:36.168: E/AndroidRuntime(529): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
02-06 19:53:36.168: E/AndroidRuntime(529): at android.app.ActivityThread.access$600(ActivityThread.java:123)
02-06 19:53:36.168: E/AndroidRuntime(529): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
02-06 19:53:36.168: E/AndroidRuntime(529): at android.os.Handler.dispatchMessage(Handler.java:99)
02-06 19:53:36.168: E/AndroidRuntime(529): at android.os.Looper.loop(Looper.java:137)
02-06 19:53:36.168: E/AndroidRuntime(529): at android.app.ActivityThread.main(ActivityThread.java:4424)
02-06 19:53:36.168: E/AndroidRuntime(529): at java.lang.reflect.Method.invokeNative(Native Method)
02-06 19:53:36.168: E/AndroidRuntime(529): at java.lang.reflect.Method.invoke(Method.java:511)
02-06 19:53:36.168: E/AndroidRuntime(529): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-06 19:53:36.168: E/AndroidRuntime(529): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-06 19:53:36.168: E/AndroidRuntime(529): at dalvik.system.NativeStart.main(Native Method)
02-06 19:53:36.168: E/AndroidRuntime(529): Caused by: java.lang.NullPointerException
02-06 19:53:36.168: E/AndroidRuntime(529): at com.sapra.converter.ConverterActivity.onCreate(ConverterActivity.java:29)
02-06 19:53:36.168: E/AndroidRuntime(529): at android.app.Activity.performCreate(Activity.java:4465)
02-06 19:53:36.168: E/AndroidRuntime(529): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
02-06 19:53:36.168: E/AndroidRuntime(529): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
02-06 19:53:36.168: E/AndroidRuntime(529): ... 11 more
public class TempTest extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(1);
Button b = new Button(this);
b.setText("get temperature");
b.setOnClickListener(new View.OnClickListener(){
public void onClick(View view) {
getCelciusOrFarenheit();
}
});
ll.addView(b);
setContentView(ll);
}
public AlertDialog getCelciusOrFarenheit(){
AlertDialog.Builder cof = new AlertDialog.Builder(this);
cof.setTitle("Select Temperature Conversion");
String[] types = {"To Celcius", "To Farenheit"};
cof.setItems(types, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
switch(which){
case 0:
createTempDialog(true).show();
break;
case 1:
createTempDialog(false).show();
break;
}
}
});
return cof.show();
}
public AlertDialog createTempDialog(final boolean cf){
final EditText et = new EditText(this);
et.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_DECIMAL);
final Formatter f = new Formatter();
return new AlertDialog.Builder(this).setView(et).setTitle(cf?"Enter Farenheit temperature":"Enter Celcius temperature").setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
double answer = 0;
if (cf) {
answer = (Double.parseDouble(et.getText().length() > 1 ? et.getText().toString() : "0") - 32) * 5 / 9;
} else {
answer = Double.parseDouble(et.getText().length() > 1 ? et.getText().toString() : "0")*9/5 + 32;
}
new AlertDialog.Builder(TempTest.this).setMessage(f.format("%.2f", answer).toString()).setTitle(cf?"Celcius":"Farenheit")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
})
.create().show();
}
}).create();
}
}
// This is the TextView you set in your layout. It will display the temperature.
final TextView tv = (TextView) findViewById(R.id.tv);
// This sets up the EditText so you can capture the input and return the temperature.
final EditText edittext = (EditText) findViewById(R.id.et);
edittext.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN)
&& (keyCode == KeyEvent.KEYCODE_ENTER)) {
// Perform action on key press
float f = Float.parseFloat(edittext.getText().toString());
tv.setText(String.valueOf(convertCelciusToFahrenheit(f)));
return true;
}
return false;
}
});
}
// Converts to celcius
private float convertFahrenheitToCelcius(float fahrenheit) {
return ((fahrenheit - 32) * 5 / 9);
}
// Converts to fahrenheit
private float convertCelciusToFahrenheit(float celsius) {
return ((celsius * 9) / 5) + 32;
}