在Android刷卡菜单中设置文字

时间:2014-08-19 19:43:32

标签: android android-layout

我正在尝试在xml页面中更改textview,这会触发android的滑动菜单。我尝试将代码放在main activity的oncreate方法中,但它在启动时崩溃了。我有另一个类,但它调用oncreateview而不是oncreate,并且onviewate()中不存在findviewbyid方法。

我在创建这个应用程序时实现了android的滑动菜单。它有两个页面,一个输入页面和一个输出页面。我在输入页面上填写了textview,edittexts和按钮。我不知道在哪里更改textview。我的目标是将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;
}
}

2 个答案:

答案 0 :(得分:1)

据我了解,您希望更改片段中的文字。您应该在片段类中处理碎片中的所有内容。所以你应该在你的片段类中更改它&#39; onCreateView方法或者您可以存储rootView并稍后进行任何更改。

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

答案 1 :(得分:1)

关于这个声明:

  

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

您可以通过构造函数将主活动的Context传递给其他类,并通过将Context强制转换为Activity来调用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