我构建了具有4种布局的介绍性滑块。我想更改一张幻灯片中的TextView
文本,但是代码不起作用(活动不知道任何视图)。我该怎么办?
IntroActivity.java:
public class IntroActivity extends AppIntro {
@Override
protected void onCreate (@Nullable Bundle savedInstanceState){
super.onCreate(savedInstanceState);
final LayoutInflater layoutInflater = getLayoutInflater();
addSlide(IntroSlide.newInstance(R.layout.slide_layout1));
addSlide(IntroSlide.newInstance(R.layout.slide_layout2));
addSlide(IntroSlide.newInstance(R.layout.slide_layout3));
addSlide(IntroSlide.newInstance(R.layout.slide_layout4));
View view = layoutInflater.inflate(R.layout.slide_layout1,null);
TextView tvSlide1 = view.findViewById(R.id.tvSlide1Title);
tvSlide1.setText("Text changed!");
}
}
IntroSlide.java:
public class IntroSlide extends Fragment {
private static final String ARG_LAYOUT_RES_ID = "layoutResId";
private int layoutResId;
public static IntroSlide newInstance(int layoutResId) {
IntroSlide introSlide = new IntroSlide();
Bundle args = new Bundle();
args.putInt(ARG_LAYOUT_RES_ID, layoutResId);
introSlide.setArguments(args);
return introSlide;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null && getArguments().containsKey(ARG_LAYOUT_RES_ID))
layoutResId = getArguments().getInt(ARG_LAYOUT_RES_ID);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
return inflater.inflate(layoutResId, container, false);
}
}
答案 0 :(得分:0)
如下所示修改您的“ onCreateView()”
TextView tvSlide1;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(layoutResId, container, false);
tvSlide1 = view.findViewById(R.id.tvSlide1Title);
}
public void setSlideText(String text){
tvSlide1.setText(text);
}
并在您的 IntroActivity.java
中IntroSlide slide1 = IntroSlide.newInstance(R.layout.slide_layout1)
addSlide(slide1);
slide1.setSlideText("some text");