图像按钮不起作用

时间:2014-08-15 07:05:06

标签: android android-launcher android-imagebutton

我是Android编程的新手,我正在尝试创建一个简单的主屏幕替换/启动器。

应用程序运行正常并且视图寻呼机正常工作,但出于某种原因,当我按下任何图像按钮时没有任何反应。

这是我的主要活动java类。

package com.dntmdp.matthewhuckel.simplephonelauncher;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;


public class MainActivity extends Activity {

    Button phone;

    Button sms;

    Button people;

    Button camera;

    Button chrome;

    Button gallery;

    Button mail;

    Button calendar;

    Button facebook;

    Button map;

    Button clock;

    Button setting;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.swipe1);

        ImageButton phone = (ImageButton) findViewById(R.id.imageButton);

        phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.android.phone");

                startActivity(LaunchIntent);
            }
        });

        ImageButton sms = (ImageButton) findViewById(R.id.imageButton2);

        sms.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.sonyericsson.conversations");

                startActivity(LaunchIntent);
            }
        });

        ImageButton people = (ImageButton) findViewById(R.id.imageButton3);

        people.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.sonyericsson.android.socialphonebook");

                startActivity(LaunchIntent);
            }
        });

        ImageButton camera = (ImageButton) findViewById(R.id.imageButton4);

        camera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.sonyericsson.android.camera");

                startActivity(LaunchIntent);
            }
        });

        setContentView(R.layout.swipe2);

        ImageButton chrome = (ImageButton) findViewById(R.id.imageButton5);

        chrome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.android.chrome");

                startActivity(LaunchIntent);
            }
        });

        ImageButton gallery = (ImageButton) findViewById(R.id.imageButton6);

        gallery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.sonyericsson.album");

                startActivity(LaunchIntent);
            }
        });

        ImageButton mail = (ImageButton) findViewById(R.id.imageButton7);

        mail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.android.email");

                startActivity(LaunchIntent);
            }
        });

        ImageButton calendar = (ImageButton) findViewById(R.id.imageButton8);

        calendar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.android.calendar");

                startActivity(LaunchIntent);
            }
        });

        ImageButton facebook = (ImageButton) findViewById(R.id.imageButton9);

        facebook.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.facebook.katana");

                startActivity(LaunchIntent);
            }
        });

        ImageButton map = (ImageButton) findViewById(R.id.imageButton10);

        map.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.google.android.apps.maps");

                startActivity(LaunchIntent);
            }
        });

        setContentView(R.layout.swipe3);

        ImageButton clock = (ImageButton) findViewById(R.id.imageButton11);

        clock.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.sonyericsson.organizer");

                startActivity(LaunchIntent);
            }
        });

        ImageButton setting = (ImageButton) findViewById(R.id.imageButton12);

        setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.android.settings");

                startActivity(LaunchIntent);
            }
        });

        setContentView(R.layout.activity_main);

        MyPagerAdapter adapter = new MyPagerAdapter();
        ViewPager myPager = (ViewPager) findViewById(R.id.viewpager_layout);
        myPager.setAdapter(adapter);
        myPager.setCurrentItem(0);
    }


    @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);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

谢谢。

3 个答案:

答案 0 :(得分:2)

每当您使用setContentView切换布局时,您必须再次重新声明图像按钮,只有当它在新layout中可用时才会起作用,因为如果它不在该布局中,它将不起作用。由于您使用开关layout编码启动器,这种方式很糟糕,请考虑使用Fragment代替。 P.S上面的代码触发click很好,但正如我上面提到的,如果你切换到新的布局而没有在imageButton下重新声明setContentView,它们将无法工作。

答案 1 :(得分:-1)

imageButton = (ImageButton) findViewById(R.id.imageButton1);

imageButton.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {

    Toast.makeText(MyAndroidAppActivity.this,
                   "ImageButton is clicked!", Toast.LENGTH_SHORT).show();

    }

});

尝试上面正在运行的代码。您不需要使用

new View.OnClickListener(){
    @Override
        public void onClick(View arg0) {
    }
}

答案 2 :(得分:-1)

检查以下代码,它对我有用,所以我认为它可以与你合作!:

public class MainActivity extends Activity implements OnClickListener {
    ImageView imv_Load;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imv_Load = (ImageView) findViewById(R.id.imv_Click);
        imv_Load.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.imv_Click:
            Toast.makeText(getApplicationContext(), "Button Clikced",
                    Toast.LENGTH_SHORT).show();
            break;

        default:
            break;
        }
    }
}