3

我使用 OnClickListener 链接了 menu.java 文件中的所有按钮,并将其定义在 Manifest 中,但按钮不起作用。我想我可能把它放在清单中是错误的。

菜单.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;
        }


        }
    }

对应的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);
    }

}

我为清单中的每个按钮添加的内容:

<activity android:name=".Help"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="com.invoice.help" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            </activity> 
4

1 回答 1

1

您需要startActivity在每个按钮单击时调用启动 Activity 的方法:

public void onClick(View v) {
        switch (v.getId()) {
        case R.id.button1:
            //do button1 action

            // start next activity here
            Intent intent=new Intent(menu.this,Job.class);
            startActivity(intent);
            break;
         ///.... same for all buttons 

        }
   }

并且您必须先注册所有 Activity,AndroidManifest.xml然后才能将其用作:

<activity android:name=".menu"
   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=".Help" />
<activity android:name=".Job" />
//.....same for others
于 2013-01-27T05:24:00.887 回答