在Android开发中需要一些从活动切换到活动的帮助。我正试图从我的MainActivity活动转到我的MainMenu活动。我实现了一个onclicklistener来完成这项工作。当我在模拟器上运行应用程序时,它运行正常但是当我单击使用onclicklistener实现的按钮时,它什么都不做。
package com.example.bmiworking;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener{
/** Called when the activity is first created. */
Button btn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.homeClickHandler);
}
public void onClick(View v){
if(v.getId() == R.id.homeClickHandler){
startActivity(new Intent(this,MainMenu.class));
}
}
public void calculateClickHandler(View view) {
// make sure we handle the click of the calculator button
if (view.getId() == R.id.calculateButton) {
// get the references to the widgets
EditText weightText = (EditText)findViewById(R.id.weightText);
EditText heightText = (EditText)findViewById(R.id.heightText);
TextView resultText = (TextView)findViewById(R.id.resultLabel);
// get the users values from the widget references
float weight = Float.parseFloat(weightText.getText().toString());
float height = Float.parseFloat(heightText.getText().toString());
// calculate the bmi value
float bmiValue = calculateBMI(weight, height);
// interpret the meaning of the bmi value
String bmiInterpretation = interpretBMI(bmiValue);
// now set the value in the result text
resultText.setText(bmiValue + "-" + bmiInterpretation);
}
}
// the formula to calculate the BMI index
// check for http://en.wikipedia.org/wiki/Body_mass_index
private float calculateBMI (float weight, float height) {
return (float) (weight * 4.88 / (height * height));
}
// interpret what BMI means
private String interpretBMI(float bmiValue) {
if (bmiValue < 16) {
return "Severely Underweight - See Weight Gain";
} else if (bmiValue < 18.5) {
return "Underweight - See Weight Gain";
} else if (bmiValue < 25) {
return "Normal - No Recomendations";
} else if (bmiValue < 30) {
return "Overweight - See Weight Loss";
} else {
return "Obese - See Weight Loss";
}
}
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="BMI Calculator"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/weightText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true"
android:ems="10"
android:inputType="numberDecimal" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:text="@string/weightLabel"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/weightText"
android:layout_centerHorizontal="true"
android:text="@string/heightLabel"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/heightText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/weightText"
android:layout_below="@+id/textView3"
android:ems="10"
android:inputType="numberDecimal" />
<Button
android:id="@+id/calculateButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/heightText"
android:layout_centerHorizontal="true"
android:onClick="calculateClickHandler"
android:text="@string/calculateButton" />
<TextView
android:id="@+id/resultLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/calculateButton"
android:layout_marginLeft="30dp"
android:layout_marginTop="15dp"
android:text="@string/emptyString"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/homeClickHandler"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/homeButton" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bmiworking"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.bmiworking.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.bmiworking.MainMenu"
android:label="@string/app_name" >
</activity>
</application>
</manifest>
答案 0 :(得分:2)
您导入了错误的OnClickListener,因此请删除该导入并删除DialogInterface onClick方法。
import android.view.View.OnClickListener;
“btn =(Button)findViewById(R.id.homeClickHandler);”和
btn.setOnClickListener(this);
达成:
package com.example.bmiworking;
import android.app.Activity;
import android.content.DialogInterface;
import android.view.View.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
Button btn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.homeClickHandler);
btn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.homeClickHandler) {
startActivity(new Intent(this, MainMenu.class));
}
}
public void calculateClickHandler(View view) {
// make sure we handle the click of the calculator button
if (view.getId() == R.id.calculateButton) {
// get the references to the widgets
EditText weightText = (EditText) findViewById(R.id.weightText);
EditText heightText = (EditText) findViewById(R.id.heightText);
TextView resultText = (TextView) findViewById(R.id.resultLabel);
// get the users values from the widget references
float weight = Float.parseFloat(weightText.getText().toString());
float height = Float.parseFloat(heightText.getText().toString());
// calculate the bmi value
float bmiValue = calculateBMI(weight, height);
// interpret the meaning of the bmi value
String bmiInterpretation = interpretBMI(bmiValue);
// now set the value in the result text
resultText.setText(bmiValue + "-" + bmiInterpretation);
}
}
// the formula to calculate the BMI index
// check for http://en.wikipedia.org/wiki/Body_mass_index
private float calculateBMI(float weight, float height) {
return (float) (weight * 4.88 / (height * height));
}
// interpret what BMI means
private String interpretBMI(float bmiValue) {
if (bmiValue < 16) {
return "Severely Underweight - See Weight Gain";
} else if (bmiValue < 18.5) {
return "Underweight - See Weight Gain";
} else if (bmiValue < 25) {
return "Normal - No Recomendations";
} else if (bmiValue < 30) {
return "Overweight - See Weight Loss";
} else {
return "Obese - See Weight Loss";
}
}
}