0

嘿伙计们,我需要帮助

另一个按钮需要打开设备上已安装的应用程序

最后一个按钮需要打开默认网络浏览器并打开google.com

这是我的代码!

主页.java:

package com.dva.schooltoolshome;

    import java.util.Locale;
    import android.app.ActionBar;
    import android.app.FragmentTransaction;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentPagerAdapter;
    import android.support.v4.view.ViewPager;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ImageButton;
    import android.widget.TextView;



    public class Home extends FragmentActivity implements ActionBar.TabListener {



        SectionsPagerAdapter mSectionsPagerAdapter;
        ViewPager mViewPager;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_home);

            final ActionBar actionBar = getActionBar();
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
            actionBar.setDisplayShowHomeEnabled(false);
            actionBar.setDisplayOptions(actionBar.getDisplayOptions()
                    ^ ActionBar.DISPLAY_SHOW_TITLE);

            mSectionsPagerAdapter = new SectionsPagerAdapter(
                    getSupportFragmentManager());


            mViewPager = (ViewPager) findViewById(R.id.pager);
            mViewPager.setAdapter(mSectionsPagerAdapter);


            mViewPager
                    .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                        @Override
                        public void onPageSelected(int position) {
                            actionBar.setSelectedNavigationItem(position);
                        }
                    });

            for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {

                if (mSectionsPagerAdapter.getPageTitle(i).toString()
                        .equalsIgnoreCase("calc")) {
                    actionBar.addTab(actionBar.newTab()
                            .setIcon(R.drawable.calendar)
                            .setTabListener(this));
                } else if (mSectionsPagerAdapter.getPageTitle(i).toString()
                        .equalsIgnoreCase("home")) {
                    actionBar.addTab(actionBar.newTab().setIcon(R.drawable.home)
                            .setTabListener(this));
                } else if (mSectionsPagerAdapter.getPageTitle(i).toString()
                        .equalsIgnoreCase("drive")) {
                    actionBar.addTab(actionBar.newTab().setIcon(R.drawable.folder)
                            .setTabListener(this));
                }

            }

            mViewPager.setCurrentItem(1);
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.home, menu);

            return true;
        }

        @Override
        public void onTabSelected(ActionBar.Tab tab,
                FragmentTransaction fragmentTransaction) {
            mViewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
                case R.id.action_settings:
                    startActivity(new Intent(android.provider.Settings.ACTION_APPLICATION_SETTINGS));
                    return true;
                default:
                    return super.onOptionsItemSelected(item);
            }
        }

        @Override
        public void onTabUnselected(ActionBar.Tab tab,
                FragmentTransaction fragmentTransaction) {
        }

        @Override
        public void onTabReselected(ActionBar.Tab tab,
                FragmentTransaction fragmentTransaction) {
        }

        public class SectionsPagerAdapter extends FragmentPagerAdapter {

            public SectionsPagerAdapter(FragmentManager fm) {
                super(fm);
            }

            @Override
            public Fragment getItem(int position) {
                Fragment fragment = null;
                switch (position) {
                case 0:
                    fragment = new CalcFragment();
                    break;
                case 1:
                    fragment = new HomeFragment();
                    break;
                case 2:
                    fragment = new DriveFragment();
                    break;

                }

                return fragment;
            }

            @Override
            public int getCount() {
                return 3;
            }

            @Override
            public CharSequence getPageTitle(int position) {
                Locale l = Locale.getDefault();
                switch (position) {
                case 0:
                    return getString(R.string.title_section1).toUpperCase(l);
                case 1:
                    return getString(R.string.title_section2).toUpperCase(l);
                case 2:
                    return getString(R.string.title_section3).toUpperCase(l);
                }
                return null;
            }
        }

        public static class HomeFragment extends Fragment {
            public static final String ARG_SECTION_NUMBER = "section_number";

            public HomeFragment() {





            }

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {
                View rootView = inflater.inflate(R.layout.activity_homebar,
                        container, false);

                return rootView;
            }
        }

        public static class CalcFragment extends Fragment {
            public static final String ARG_SECTION_NUMBER = "section_number";

            public CalcFragment() {

            }

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {
                View rootView = inflater.inflate(R.layout.activity_calculator,
                        container, false);

                return rootView;
            }
        }

        public static class DriveFragment extends Fragment {
            public static final String ARG_SECTION_NUMBER = "section_number";

            public DriveFragment() {
            }

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {
                View rootView = inflater.inflate(R.layout.fragment_home_dummy,
                        container, false);
                TextView dummyTextView = (TextView) rootView
                        .findViewById(R.id.section_label);
                dummyTextView.setText("DRIVE");
                return rootView;


            }

        }
    }

活动主页栏

<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"
    android:background="@drawable/bg"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".HomeBar" >

    <ImageButton
        android:id="@+id/apps"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:background="@drawable/appdrawer"
        android:src="@drawable/appdrawer" />

    <ImageButton
        android:id="@+id/wbrowser"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignTop="@+id/apps"
        android:src="@drawable/browser" />

</RelativeLayout>

AllApps.java:

package com.dva.schooltoolshome;

import android.app.LauncherActivity;
import android.content.Intent;



public class AllApps extends LauncherActivity {

     @Override
        protected Intent getTargetIntent () {
             // just a example, you should replace the method yourself
             Intent intent = new Intent();
             intent.setAction(Intent.ACTION_MAIN);
             intent.addCategory(Intent.CATEGORY_LAUNCHER);
             intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
             return intent;
         }
    }

我还在学习所以请不要评判

提前致谢

问候

说唱11

4

1 回答 1

0

您可以通过谷歌搜索轻松找到问题的答案。之前已经问过很多次了。我会为你指出正确的方向。

要向按钮添加功能,您必须首先让按钮(视图)知道它可以被单击。这可以在java中以编程方式(自己研究)或在xml中完成,如下所示:

android:onClick="onClickDoStuff"

然后你必须实现如下onClickDoStuff所示的方法:

public void onClickDoStuff(View view) {
    //Do stuff on click
}

您想要打开一个新活动的按钮onClick,您需要intent查看这里以获得进一步的指导

于 2013-10-08T20:27:28.253 回答