Android EditText或可能是OnClickEvent问题(Android EditText or possibly OnClickEvent issue)

编程入门 行业动态 更新时间:2024-10-26 00:26:03
Android EditText或可能是OnClickEvent问题(Android EditText or possibly OnClickEvent issue)

我已经阅读了大量的问题,但无法找到有效的答案。 无论如何,我是android / java编程的新手,并且无法获取我的按钮onClick来获取我的EditText内容并相应地使用它们(?)我认为是我的问题,继承我的代码

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 Fittings extends Activity implements OnClickListener{ /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button go = (Button)findViewById(R.id.GoButton); go.setOnClickListener(this); } public void onClick(View v) { EditText circum = (EditText)findViewById(R.id.CtoD); int C = Integer.parseInt(circum.getText().toString()); TextView CtoDAnswer = (TextView)findViewById(R.id.CtoDAnswer); CtoDAnswer.setText((int) (C * 0.3183)); }

}

I've read through tons of questions and was unable to find an answer that worked. Anyway, I'm new to android/java programming, and can't get my button onClick to get my EditText contents and use them accordingly(?) I think is my problem, heres my code

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 Fittings extends Activity implements OnClickListener{ /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button go = (Button)findViewById(R.id.GoButton); go.setOnClickListener(this); } public void onClick(View v) { EditText circum = (EditText)findViewById(R.id.CtoD); int C = Integer.parseInt(circum.getText().toString()); TextView CtoDAnswer = (TextView)findViewById(R.id.CtoDAnswer); CtoDAnswer.setText((int) (C * 0.3183)); }

}

最满意答案

您正在使用CtoDAnswer.setText((int) (C * 0.3183)); ,它不会添加具有该数字的字符串,但会向其添加具有该ID的资源。 由于您没有字符串资源c * 0.3183,因此无法得到正确的答案。

使用CtoDAnswer.setText(Integer.toString(C * 0.3183));

请参阅此方法的API参考: http : //developer.android.com/reference/android/widget/TextView.html#setText%28int%29


作为第二条评论(这与您的问题无关,但是关于良好实践和应用效率的建议):每次单击按钮时都不要给资源充气,而是在onCreate内部充气并使用类字段存储它。 即:

EditText circum; // public void onCreate(Bundle savedInstanceState) { circum = (EditText)findViewById(R.id.CtoD); // public void onClick(View v) { int C = Integer.parseInt(circum.getText().toString());

You are using CtoDAnswer.setText((int) (C * 0.3183));, which does not add a string with that number, but adds a resource with that ID to it. Since you do not have a string resource c*0.3183, you won't get the right answer.

Use CtoDAnswer.setText(Integer.toString(C * 0.3183));

See the API reference for that method here: http://developer.android.com/reference/android/widget/TextView.html#setText%28int%29


As a second comment (and this is unrelated to your problem, but an advise on good practices and app efficiency): Do not inflate the resource every time you click on the button, but inflate it inside onCreate and use a class field to store it. I.e.:

EditText circum; // public void onCreate(Bundle savedInstanceState) { circum = (EditText)findViewById(R.id.CtoD); // public void onClick(View v) { int C = Integer.parseInt(circum.getText().toString());

更多推荐

本文发布于:2023-08-05 08:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1428781.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:EditText   Android   OnClickEvent   issue   possibly

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!