数据未在textview-Android中填充

时间:2014-09-24 11:43:28

标签: android eclipse android-layout

我是android app dev的初学者。首先,我尝试使用以下代码开发一个小应用程序。没有编译时或运行时错误。但它并没有接受我的输入,也没有填充输出。

package com.example.myfirstapp;

import android.os.Bundle;
//import android.view.View.OnClickListener; 
import android.app.Activity;  
import android.view.Menu;  
import android.view.View;  
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;  
import android.widget.RadioButton;  
import android.widget.TextView;
/*import android.widget.Toast;
import android.app.Activity;
import android.view.Menu;*/

public class MainActivity extends Activity implements OnClickListener{
    //private EditText text;  
    RadioButton mileButton,kmhButton ;
    TextView textview1,textview2;
    Button calcButton;
    private View.OnClickListener droidTapListener;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        System.out.println("HIIIIIIIIII");

        setContentView(R.layout.activity_main);
        //text = (EditText) findViewById(R.id.textView1); 
        }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        //getMenuInflater().inflate(R.menu.main, menu); 
        return true;
    }

    public void calculate() {  

   mileButton = (RadioButton) findViewById(R.id.radio0);  //radiobutton objects
         kmhButton = (RadioButton) findViewById(R.id.radio1);  

         calcButton = (Button) findViewById(R.id.button1);     //button obj
        calcButton.setOnClickListener(droidTapListener);   //setting up click listeners.

        TextView textview1 = (TextView) findViewById(R.id.textView1);
        TextView textview2 = (TextView) findViewById(R.id.textView2);
        /*if(text.getText().length()==0)
            System.out.println("Empty Value");*/

        if(textview1.getText().length()==0)
            System.out.println("Enter a valid input.");

        /*Toast.makeText(this, "enter a valid number", Toast.LENGTH_LONG)  
        .show(); */

        double inputValue = Double.parseDouble(textview1.getText().toString()); 
        if (mileButton.isChecked()) {  
            //text.setText(String.valueOf(convertToMiles(inputValue))); 
            double res= convertToMiles(inputValue);
            String finalresult = new Double(res).toString();
            textview2.setText(finalresult);

            System.out.println("RESULT"+finalresult);

            // uncheck "to miles" Button  
            mileButton.setChecked(false);  
            // check "to km/h" Button  
            kmhButton.setChecked(true);  
        } else { /* if kmhButton isChecked() */  
            //text.setText(String.valueOf(convertToKmh(inputValue))); 
            double res= convertToKmh(inputValue);
            String finalresult = new Double(res).toString();
            textview2.setText(finalresult);

            System.out.println("RESULT"+finalresult);

            // uncheck "to km/h" Button  
            kmhButton.setChecked(false);  
            // check "to miles" Button  
            mileButton.setChecked(true);  

        }  
    } 



private double convertToMiles(double inputValue) {  
    // convert km/h to miles 
    double res=inputValue * 1.609344;
    return res;

   //return (inputValue * 1.609344);  
}  

private double convertToKmh(double inputValue) {  
    // convert miles to km/h  
    return (inputValue * 0.621372);  
    }

public void onClick(View v) {
    calculate();
}
}

2 个答案:

答案 0 :(得分:0)

您已在compute方法中设置了onclickListener。意味着除非你的计算方法不被调用,否则onClickListener将不会调用,如果onClickListener不会被注册,则compute()将不会调用methode。

这样做

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    System.out.println("HIIIIIIIIII");

    setContentView(R.layout.activity_main);
    //text = (EditText) findViewById(R.id.textView1); 

     calcButton = (Button) findViewById(R.id.button1);    
    calcButton.setOnClickListener(droidTapListener);  

    }

答案 1 :(得分:0)

首先,你应该在onCreate()中初始化你的视图组件,所以在创建之前移动这段代码并在文本视图之前删除TypeDefinition,你已经声明了这个变量。

   mileButton = (RadioButton) findViewById(R.id.radio0);  //radiobutton objects
   kmhButton = (RadioButton) findViewById(R.id.radio1);  

   calcButton = (Button) findViewById(R.id.button1);     //button obj
   calcButton.setOnClickListener(this);   //you droidTapListener isn't initialize and you implements clickListener so use this for listening this event.
   textview1 = (TextView) findViewById(R.id.textView1);
   textview2 = (TextView) findViewById(R.id.textView2); 

不要忘记为你的onClick()函数添加@override,这样你点击按钮就会运行onclick函数来计算函数。