0

I am pretty new to android programming and I am trying to create a simple home screen replacement/launcher.

The application runs fine and the view pager works, but for some reason when I press on any of the image buttons nothing happens.

This is my main activity java class.

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);
    }
}

Thank you.

4

3 回答 3

2

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

于 2014-08-15T07:57:03.697 回答
-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) {
    }
}
于 2014-08-15T07:09:24.877 回答
-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;
        }
    }
}
于 2014-08-15T07:22:43.630 回答