Problem with Android EditText or possibly OnClickEvent

I read a lot of questions and could not find the answer that worked. Anyway, I am new to android / java programming and can't make my onClick button get the contents of EditText and use them accordingly (?) I think this 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));

}

}

+3
source share
3 answers

You use CtoDAnswer.setText((int) (C * 0.3183));one that does not add a line with this number, but adds a resource with this ID to it. Since you do not have the string resource c * 0.3183, you will not get the correct answer.

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

See the API link for this method here: 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());
+2

, @Override onClick(View v ) :

@Override
public void onClick(View v){
    //do stuff 
    EditText circum = (EditText)findViewById(R.id.CtoD);
    int C = Integer.parseInt(circum.getText().toString());
    TextView CtoDAnswer = (TextView)findViewById(R.id.CtoDAnswer);      
    CtoDAnswer.setText(Integer.toString(C * 0.3183));//this will Fix the problem
    String txtCentent = myEditText.getText():
    Log.i("Content EditText"," Content = "+txtContent);
}

:

, onCreate, , , Views By ID, .

0
public void onClick(View v) {
    EditText circum = (EditText)findViewById(R.id.CtoD);
    double C = Integer.parseInt(circum.getText().toString());
    TextView CtoDAnswer = (TextView)findViewById(R.id.CtoDAnswer);      
    double answer = (C * 0.3183);
    CtoDAnswer.setText(": " + answer);

, , , !

0

All Articles