更改TextView的内部片段 [英] Change TextView inside Fragment
本文介绍了更改TextView的内部片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我跟着这家伙的教程如何做一个动作条。比方说,我想更改里面的片段中的一个TextView的。所以我说这对我的StartActivity.java,的onCreate下:
I followed this guy's tutorial on how to make an ActionBar. Let's say I want to change the TextView inside one of the fragments. So I added this on my StartActivity.java, under onCreate:
TextView textview = (TextView) findViewById(R.id.textView1);
textview.setText("HI!");
当我开始我的应用程序,它崩溃。有人可以点我朝着正确的方向?
When I start my app, it crashes. Can somebody point me to the right direction?
我希望有人花时间看看这个家伙的教程,因为他的布局基本上和我一样。谢谢你。
I hope somebody takes the time to look at the guy's tutorial because his layout is basically the same as mine. Thank you.
推荐答案
如果你想改变你的组成部分,我建议你作出这样的片段里面的方法:
If you want change your component, I suggest you to make a method inside the fragment like this:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class DetailFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.details, container, false);
return view;
}
public void setText(String text){
TextView textView = (TextView) getView().findViewById(R.id.detailsText);
textView.setText(text);
}
}
这篇关于更改TextView的内部片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文