我已经阅读了大量的问题,但无法找到有效的答案。 无论如何,我是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());更多推荐
发布评论