我已经导入了包import android.view.View.OnClickListener;
但是在程序里写 private OnClickListener calcBMI = new OnClickListener() 提示:不能实例化类型 View.OnClickListener”
以下是源代码:
- Java code
package com.demo.android.bmi;import java.text.DecimalFormat;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 Bmi extends Activity { /** Called when the activity is first created. * @param <calcBMI>*/ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //Listen for button clicks Button button = (Button)findViewById(R.id.submit); button.setOnClickListener(calcBMI); } private OnClickListener calcBMI = new OnClickListener() public void onClick(View v){ DecimalFormat nf =new DecimalFormat("0.00"); EditText fieldheight = (EditText)findViewById(R.id.height); EditText fieldweight = (EditText)findViewById(R.id.weight); double height = Double.parseDouble(fieldheight.getText().toString())/100; double weight = Double.parseDouble(fieldheight.getText().toString()); double BMI = weight / (height * height); TextView result = (TextView)findViewById(R.id.result); result.setText("YourBMIis"+nf.format(BMI)); //Give health advice TextView fieldsuggest = (TextView)findViewById(R.id.suggest); if(BMI>25){ fieldsuggest.setText(R.string.advice_heavy); } else if(BMI<20){ fieldsuggest.setText(R.string.advice_light); } else { fieldsuggest.setText(R.string.advice_average); } } }
------解决方案--------------------
public interface OnClickListener {}
这是个接口类, 当然不能实例化了 --!
你需要新建一个类去实现这个接口
------解决方案--------------------
public interface OnClickListener {}
这是个接口类, 当然不能实例化了 --!
你需要新建一个类去实现这个接口