【问题标题】:Setting Text in Android Swipe Menu在 Android 滑动菜单中设置文本
【发布时间】:2014-08-19 19:43:31
【问题描述】:

我正在尝试更改 xml 页面中的 textview 以暗示 android 的滑动菜单。我尝试将代码放在主要活动的 oncreate 方法中,但它在启动时崩溃。我确实有另一个类,但它调用 oncreateview 而不是 oncreate,并且在 oncreateview() 中不存在 findviewbyid 方法。

我在创建这个应用程序时实现了 android 的滑动菜单。它有两个页面,一个输入页面和一个输出页面。我已经用文本视图、编辑文本和按钮填充了输入页面。我不确定在哪里更改文本视图。我的目标是将 textview 设置为今天的日期。

ViewPager mViewPager;

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



    // Set up the action bar.
    final ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the app.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    // When swiping between different sections, select the corresponding
    // tab. We can also use ActionBar.Tab#select() to do this if we have
    // a reference to the Tab.
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }
    });

    // For each of the sections in the app, add a tab to the action bar.
    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
        // Create a tab with text corresponding to the page title defined by
        // the adapter. Also specify this Activity object, which implements
        // the TabListener interface, as the callback (listener) for when
        // this tab is selected.
        actionBar.addTab(
                actionBar.newTab()
                        .setText(mSectionsPagerAdapter.getPageTitle(i))
                        .setTabListener(this));
    }

  TextView tv = (TextView) findViewById(R.id.textView4);
 // tv.setText("test post");
//      tv.setText("test post");
}

public class inputClass extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.input_xml, container, false);

    return rootView;
}
}

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    据我了解,您想更改片段中的文本。您应该在片段类中处理有关片段中的所有内容。所以你应该在你的片段类的 onCreateView 方法中改变它,或者你可以存储你的 rootView 并在以后做任何类似的改变。

    ((TextView) rootView.findViewById( R.id.yourId )).setText( "Hello from Turkiye" );
    

    【讨论】:

    • 如果我将 changetext 代码放在 onCreateOptionsMenu 中,也可以在 onCreate 中的代码之后使用。
    • Android 在某些方面很灵活。例如,在调用 onCreateOptionsMenu 方法后,您可以在任何地方膨胀菜单。但建议以该方法设置您的菜单。有一些建议的方法来做事,你当然可以不遵守规则。有多种方法可以处理片段。一种是将 rootView 传递给活动,将其存储在您的活动类中并在那里进行处理。但不建议这样做。你应该在你的片段活动中处理你的片段。
    【解决方案2】:

    关于此声明:

    我确实有另一个类,但它调用 oncreateview 而不是 oncreate 而且oncreateview()中不存在findviewbyid方法。

    您可以通过它的构造函数将您的主要活动的上下文传递给您的其他类,并通过将上下文转换为活动来调用 findViewById 方法。在你的课堂上有这样的事情。

    Context mContext;
    
    public yourconsructor(Context context)
         {
         mContext = context;
         }
    
    View view = ((Activity)mContext).findViewById(R.id.yourview)
    

    这里也有更多:

    using findviewbyid in a class that does NOT extend Activity in android

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-09
      • 1970-01-01
      • 2012-05-31
      • 2014-09-02
      • 1970-01-01
      • 2012-05-30
      • 2013-05-28
      • 2013-06-03
      相关资源
      最近更新 更多