在我的 main.xml 中,我有 5 个按钮,所有这些按钮都通向不同的页面,但是当我启动应用程序时,这些按钮是它们的,但是当单击它们时它们不起作用。我认为问题可能出在 AndroidManifest.xml
菜单.Java
package com.invoice;
import android.app.Activity;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.view.View;
import android.widget.Button;
public class menu extends Activity implements OnClickListener {
/** called the activity is first created. */
Button button1, button2, button3, button4, button5;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1 = (Button)findViewById(R.id.button1);
button2 = (Button)findViewById(R.id.button2);
button3 = (Button)findViewById(R.id.button3);
button4 = (Button)findViewById(R.id.button4);
button5 = (Button)findViewById(R.id.button5);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
button5.setOnClickListener(this);
}
public void onClick(View v) {
//TODO Auto-generate method stub
//figure out which button was pressed
switch (v.getId()) {
case R.id.button1:
//do button1 action
break;
case R.id.button2:
//do button2 action
break;
case R.id.button3:
//do button3 action
break;
case R.id.button4:
//do button4 action
break;
case R.id.button5:
//do button5 action
break;
}
}
}
Android Manifest(仅按钮活动)
android:name=".menu"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.invoice.MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name = ".Help" />
<activity android:name = ".Job" />
<activity android:name=".Fourm" />
<activity android:name=".Receipt"/>
<activity android:name=".Reportissue"/>
Job.java(相同的代码在每个按钮的 java 文件中)
package com.invoice;
import android.app.Activity;
import android.os.Bundle;
public class Job extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.job);
}
}
主.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp" >
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="87dp"
android:text="@string/report_issue"
android:width="300dp" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button3"
android:layout_marginBottom="29dp"
android:text="@string/help"
android:width="300dp" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button2"
android:layout_alignLeft="@+id/button3"
android:layout_marginBottom="36dp"
android:text="@string/fourm"
android:width="300dp" android:height="50dp"/>
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/button1"
android:layout_marginTop="15dp"
android:height="150dp"
android:text="@string/reciept"
android:width="145dp" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button5"
android:layout_alignBottom="@+id/button5"
android:layout_toLeftOf="@+id/button5"
android:height="150dp"
android:text="@string/job"
android:width="150dp" />
<DigitalClock
android:id="@+id/digitalClock1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="38dp"
/>
</RelativeLayout>