我是Android應用開發者的初學者。首先,我試着用下面的代碼開發一個小應用程序。沒有編譯時或運行時錯誤。但它沒有考慮到我的輸入,也沒有獲得輸出。數據沒有被填充在textview-Android中
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();
}
}
你沒有在onCreate()中調用你的calculate()...並且還使用Toast而不是println ..並且使用按鈕來設置onClickListener..so在按鈕上單擊可以調用calculate方法並檢查文本ID填充或null :) – Ranjit 2014-09-24 11:44:47
首先在'onCreate()'方法上調用'calculate()'並且改變這個'String finalresult = String.valueOf(res);' – Piyush 2014-09-24 11:47:47
你似乎誤解了幾個核心概念Android編程。爲什麼不試圖通過閱讀一本書(Reto Meier的專業Android 4應用程序開發很好)或在線課程(如https://www.udacity.com/course/ud853)來學習基礎知識? – 2Dee 2014-09-24 11:51:03