2013-10-08 68 views
0

嗨,我需要幫助,我正在做,需要有一個按鈕,單擊它時打開AllApps.class活動女巫的啓動與啓動按鈕點擊一個活動顯示所有已安裝的應用程序的列表使用FragmentActivity

其他按鈕需要打開一個安裝的應用程序是設備

和最後一個按鈕就需要打開默認的Web瀏覽器並打開google.com

這裏是我的代碼!

Home.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; 


      } 

     } 
    } 

activity_homebar

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

我還在學習,所以請不要判斷

在此先感謝

問候

Rapsong11

+2

什麼問題? – codeMagic

+0

@codeMagic我需要一個按鈕,當它點擊它打開allapps.java類 – rapsong11

+0

好吧,你堅持哪部分?你創建了「Button」嗎?您是否嘗試添加功能? – codeMagic

回答

0

你可以很容易地找到問題的答案有一點谷歌搜索。之前已經問過很多次了。我會指出你正確的方向。

要添加功能的按鈕,你必須先按鈕(查看)意識到它可以點擊。這可以在Java編程來完成(此研究自己)或XML像這樣:

android:onClick="onClickDoStuff" 

然後,你必須實現onClickDoStuff方法,它看起來像:

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

你想要的按鈕開一個新的活動onClick要做到這一點,你會需要一個intent看看here進一步指導

相關問題