我试图在主要活动中设置我的片段文件中的文本。这是我的代码
这是我的mainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = new SQLiteHandler(getApplicationContext());
session = new SessionManager(getApplicationContext());
if (!session.isLoggedIn()) {
logoutUser();
}
HashMap<String, String> user = db.getUserDetails();
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.main_container, new HomeFragment());
fragmentTransaction.commit();
getSupportActionBar().setTitle("Home Fragment");
navigationView = (NavigationView)findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.home_id:
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_container, new HomeFragment());
fragmentTransaction.commit();
getSupportActionBar().setTitle("Home Fragment");
item.setChecked(true);
break;
case R.id.help_id:
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_container, new HelpFragment());
fragmentTransaction.commit();
getSupportActionBar().setTitle("Help Fragment");
item.setChecked(true);
break;
case R.id.logout:
logoutUser();
}
return false;
}
});
}
我有这个(HomeFragment.java)
public class HomeFragment extends Fragment {
public HomeFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_home, container, false);
}
}
我在我的fragment_home.xml
上有这个<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="@color/lbl_name"
android:textSize="24dp" />
我也可以设置&#39; name&#39;来自mainActivity?如果有,请告诉我怎么做?
答案 0 :(得分:0)
添加片段时,您可以为其指定标记:
fragmentTransaction.add(R.id.main_container, new HomeFragment(), "MY_FRAGMENT");
fragmentTransaction.commit();
当您需要更改TextView
时,可以使用used标签恢复片段,并调用更改文本的片段方法:
HomeFragment fragment = (HomeFragment) getSupportFragmentManager()
.findFragmentByTag("MY_FRAGMENT");
fragment.changeText("New text");
如果您没有使用支持库,请将getSupportFragmentManager()
替换为getFragmentManager()
。
HomeFragment
类中的方法是:
public void changeText(String newText) {
TextView textview = (TextView) getView().findViewById(R.id.name);
textview.setText(newText);
}
答案 1 :(得分:-1)
这是您的错误,您没有为textview ing创建视图提供任何视图,请尝试此
HomeFragment Class
public static TextView mTextView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
mTextView=(TextView)view.findviewById(R.id.name);
return view;
}
在你的班级活动中
添加片段调用之后
HomeFragment.mTextView.setText("Name");
试试上面的代码