2014-12-09 73 views
0

在我的Android應用程序,我有一個變量在我的活動頁面如下圖所示:如何訪問活動變到java類

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.fragment_sample); 
     String pageTitle = "abc"; 
} 

,我想訪問該pageTitle可變進我的java類象下面這樣:

public class SlidingTabLayout extends HorizontalScrollView { 

    /** 
    * Allows complete control over the colors drawn in the tab layout. Set with 
    * {@link #setCustomTabColorizer(TabColorizer)}. 
    */ 
    public interface TabColorizer { 

     /** 
     * @return return the color of the indicator used when {@code position} is selected. 
     */ 
     int getIndicatorColor(int position); 

     /** 
     * @return return the color of the divider drawn to the right of {@code position}. 
     */ 
     int getDividerColor(int position); 

    } 

    private static final int TITLE_OFFSET_DIPS = 24; 
    private static final int TAB_VIEW_PADDING_DIPS = 16; 
    private static final int TAB_VIEW_TEXT_SIZE_SP = 12; 

    private int mTitleOffset; 

    private int mTabViewLayoutId; 
    private int mTabViewTextViewId; 

    private ViewPager mViewPager; 
    private ViewPager.OnPageChangeListener mViewPagerPageChangeListener; 
    String value = ""; 
    private final SlidingTabStrip mTabStrip; 

    public SlidingTabLayout(Context context) { 
     this(context, null); 
    } 

    public SlidingTabLayout(Context context, AttributeSet attrs) { 
     this(context, attrs, 0); 
    } 

    public SlidingTabLayout(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 

     // Disable the Scroll Bar 
     setHorizontalScrollBarEnabled(false); 
     // Make sure that the Tab Strips fills this View 
     setFillViewport(true); 

     mTitleOffset = (int) (TITLE_OFFSET_DIPS * getResources().getDisplayMetrics().density); 

     mTabStrip = new SlidingTabStrip(context); 
     addView(mTabStrip, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 
    } 

And I want to access variable in this method 

private void populateTabStrip() { 
     final PagerAdapter adapter = mViewPager.getAdapter(); 
     final View.OnClickListener tabClickListener = new TabClickListener(); 

     for (int i = 0; i < 5 ; i++) {//adapter.getCount(); i++) { 
      View tabView = null; 
      TextView tabTitleView = null; 

      if (mTabViewLayoutId != 0) { 
       // If there is a custom tab view layout id set, try and inflate it 
       tabView = LayoutInflater.from(getContext()).inflate(mTabViewLayoutId, mTabStrip, 
         false); 
       tabTitleView = (TextView) tabView.findViewById(mTabViewTextViewId); 
      } 

      if (tabView == null) { 
       tabView = createDefaultTabView(getContext()); 
      } 

      if (tabTitleView == null && TextView.class.isInstance(tabView)) { 
       tabTitleView = (TextView) tabView; 
      } 

      tabTitleView.setText(pageTitle); // here I am putting `pageTitle` variable 
      tabView.setOnClickListener(tabClickListener); 

      mTabStrip.addView(tabView); 
     } 
    } 

} 
+1

只是要'公共靜態字符串PAGETITLE =「abc」;'並使用任何類似於'your_Activity.pageTitle'的地方 – 2014-12-09 12:02:39

+1

將值傳遞給構造函數或在您的java類中創建setter – Amith 2014-12-09 12:03:55

+0

我想將此變量用於我的java類 – user88 2014-12-09 12:05:30

回答

2

可以聲明的PageTitle作爲活動的靜態成員,然後在你的java類,如下使用它:

public MyActivity extends Activity{ 
    public static String pageTitle = ""; 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.fragment_sample); 
     pageTitle = "abc"; 
    } 
} 

而且在Java類

private void populateTabStrip() { 
      ... 
      tabTitleView.setText(MyActivity.pageTitle); 
      tabView.setOnClickListener(tabClickListener); 

      mTabStrip.addView(tabView); 

} 
0

由您創建的變量String pageTitle = "abc";是方法onCreateLocal Variable,因爲您已在方法內創建它。

如果您想在方法外使用它,則需要在onCreate之外聲明它。

現在根據你的需要,你可以聲明pageTitle是一個static(類)變量並使用類名稱訪問它,像,YourActivityName.pageTitle或者您可以在此pageTitle的值傳遞給其他類,並使其保持在任何其他變量。

0

爲SlidingTabLayout類創建一個構造函數,在那裏傳遞參數並在超級後設置它。

+3

這是一個相當簡短的答案。您是否願意詳細闡述一下,或者展示一些示例代碼? – GameDroids 2014-12-09 13:14:57