gpt4 book ai didi

java - 不要将文本显示与设置的文本连接起来,而是使用 android 资源?

转载 作者:行者123 更新时间:2023-11-30 10:51:12 25 4
gpt4 key购买 nike

 @Override
public View onCreateView(LayoutInflater inflater,@Nullable ViewGroup container,@Nullable Bundle savedInstanceState) {
View layout=inflater.inflate(R.layout.my_fragment, container, false);
textview=(TextView)layout.findViewById(R.id.position);
Bundle bundle=getArguments();
if(bundle!=null){
System.out.println("not null");
textview.setText("The Page Currently Selected is"+bundle.getInt("position"));
}
System.out.println(" null");
return layout;

在上面的代码中,它说 DO NOT CONCATENATE TEXT DISPLAY WITH SET TEXT, USE ANDROID RESOURCES INSTEAD, also, my viewpager is not working, it is not showing my fragment, I believe it has something do with this, any建议?下面是我的主要 Activity 的其余部分,我设置了我的 viewpager,但它不能与我的 fragment 一起工作,或者当我加载它时????

public class MainActivity extends ActionBarActivity {
private Toolbar mToolbar;
private ViewPager mPager;
private SlidingTabLayout mTabs;
private static final String KEY_FRAGMENT="position";


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

mToolbar=(Toolbar)findViewById(R.id.app_bar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
NavigationFragment drawerlayout=(NavigationFragment)
getSupportFragmentManager().findFragmentById(R.id.fragment_navigation);
drawerlayout.setUp(R.id.fragment_navigation,(DrawerLayout)findViewById(R.id.drawer_layout),mToolbar);
mPager=(ViewPager)findViewById(R.id.pager);
mPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
mTabs=(SlidingTabLayout)findViewById(R.id.tabs);
mTabs.setViewPager(mPager);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
if(id==R.id.navigate){
startActivity(new Intent(this, NavigateTo.class));
}

return super.onOptionsItemSelected(item);
}


class MyPagerAdapter extends FragmentPagerAdapter {
String[]tabs;

public MyPagerAdapter(FragmentManager fm) {
super(fm);
tabs=getResources().getStringArray(R.array.tabs);
}

@Override
public Fragment getItem(int position) {
MyFragment myFragment=MyFragment.getInstance(position);

return myFragment;
}

@Override
public CharSequence getPageTitle(int position) {
return tabs[position];
}

@Override
public int getCount() {
return 3;
}
//constructing a fragment class in the MainActivity(UPDATE THIS)
}
public static class MyFragment extends Fragment{
private TextView textview;
//in position represents the position that is shown inside the fragment
public static MyFragment getInstance(int position){
MyFragment myFragment=new MyFragment();
Bundle args=new Bundle();
//UPDATE PUT A KEY IN FOR "position"
args.putInt(KEY_FRAGMENT,position);
myFragment.setArguments(args);
return myFragment;
}

@Override
public View onCreateView(LayoutInflater inflater,@Nullable ViewGroup container,@Nullable Bundle savedInstanceState) {
View layout=inflater.inflate(R.layout.my_fragment, container, false);
textview=(TextView)layout.findViewById(R.id.position);
Bundle bundle=getArguments();
if(bundle!=null){
System.out.println("not null");
textview.setText("The Page Currently Selected is"+bundle.getInt("position"));
}
System.out.println(" null");
return layout;

}
}
}

最佳答案

这里有两件事。

首先,您应该使用资源字符串和格式化方法。在 res/strings.xml 中添加当前行

<string name="resource_id">"The Page Currently Selected is %d$1"</string>

因此,在您的代码中您可以使用 getString 方法

getResources().getString(R.id._resource_id, bundle.getInt("position"));

更多信息在这里:http://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling

对于第二点,和之前的警告没有联系,应该还有我没注意到的地方。也许您的 textView 的颜色与您的背景颜色相同,使其不可见。

关于java - 不要将文本显示与设置的文本连接起来,而是使用 android 资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34793276/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com