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



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

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

我想在我的 java 类中访问这个pageTitle变量,如下所示:

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

您可以将 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);
}

为 SlidingTabLayout 类创建一个构造函数,将参数传入其中,并在 super 之后设置它。

创建的变量String pageTitle = "abc";是方法onCreateLocal Variable,因为您在方法中创建它。

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

现在,根据您的

需要,您可以将pageTitle声明为static(类(变量并使用类名访问它,例如,YourActivityName.pageTitle或者您可以将此pageTitle的值传递给另一个类并将其保存在任何其他变量中。

最新更新