如何将活动变量访问到java类中

时间:2014-12-09 11:59:50

标签: java android

在我的Android应用程序中,我的活动页面中有一个变量,如下所示:

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

}

3 个答案:

答案 0 :(得分: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);

}

答案 1 :(得分:0)

您创建的变量String pageTitle = "abc";是方法Local Variable的{​​{1}},因为您已在方法中创建了它。

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

现在,根据您的需要,您可以将onCreate声明为pageTitle(类)变量,并使用类名static来访问它,或者您可以传递值这个YourActivityName.pageTitle到另一个类中,并将其保存在任何其他变量中。

答案 2 :(得分:0)

为SlidingTabLayout类创建一个构造函数,在那里传递参数并在super之后设置它。