单击按钮时如何更新片段中的TextView?

时间:2016-05-10 20:54:24

标签: android android-fragments fragment

我有一个包含相应代码的片段:

public class Fragment1 extends Fragment {

public static Fragment1 newInstance() {
    Fragment1 fragment = new Fragment1();
    return fragment;
}

public Fragment1() {
    // Deve existir um construtor vazio
    // na classe que estende um Fragment
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.activity_fragment1, container, false);
}

public void changeText(){
    //this textview should be bound in the fragment onCreate as a member variable
    TextView frv= (TextView) getView().findViewById(R.id.msgStatusPorta);
    frv.setText("Porta Atualmente Aberta");

}

在我的MainActivity中,我这样做:

FragmentManager fm = getSupportFragmentManager(); //if you added fragment via layout xml 
Fragment1 fragment = (Fragment1)fm.findFragmentById(R.id.fragment1); 

在onClick中,我做

fragment.changeText(view);

我尝试从方法changeText()更新我的Textview但是当我点击按钮并调用此方法时,我的应用程序崩溃了。有人可以帮帮我吗?

感谢。

4 个答案:

答案 0 :(得分:0)

你有没有把片段放到xml中,然后你可以这样做:

private MyFrag fragment; //MyFrag is your fragmemt
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    fragment = (MyFrag)
            getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);

    Button yourbutton = (Button) findViewById(R.id.yourbutton);
    yourbutton.setOnClickListener(new View.OnClickListener(){
      public void onClick(View view){
            fragment.setEditText("Your Text");
      });

在你的片段中:

 public void setEditText(String text){
     edittext.setText(text);
 }

答案 1 :(得分:0)

在您的片段中实施onViewCreated()并初始化您的TextView -

private TextView textView;
 @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

       textView = (TextView)view.findViewById(R.id.msgStatusPorta);
}

并将changeText()更改为 -

public void changeText(){
    //this textview should be bound in the fragment onCreate as a member variable

    textView.setText("Porta Atualmente Aberta");

}

答案 2 :(得分:0)

在Fragment类上添加onActivityCreated方法,然后查找编辑文本视图。创建主机活动时,在onActivityCreated方法之后调用onCreateView()方法。已创建ActivityFragment实例以及活动的视图层次结构。此时,可以使用findViewById()方法访问视图。

private TextView frv;
private Button yourbutton;
...

public void onActivityCreated(Bundle savedInstanceState) {
  View rooView = getView();
  frv= (TextView)rooView.findViewById(R.id.msgStatusPorta);
  yourbutton = (Button)rooView.findViewById(R.id.your_button_res_id);
  ...
}

答案 3 :(得分:0)

您可以使用界面轻松完成。在你的活动中做这样的事情

public class YourActivity extends AppCompatActivity{
    FragmentCommunicator fragmentCommunicator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                fragmentCommunicator.changeText("Hello");

            }
        });

    }

    public void passVal(FragmentCommunicator fragmentCommunicator) {
        this.fragmentCommunicator = fragmentCommunicator;

    }

}

public interface FragmentCommunicator {
   public void changeText(String textVal);
}

在你的片段中做这样的事情。

@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup  container, @Nullable Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.activity_fragment1, container, false)
    ((YourActivity) getActivity()).passVal(new FragmentCommunicator() {
        @Override
        public void changeText(String textVal) {  
               TextView frv = (TextView) view.findViewById(R.id.msgStatusPorta);
               frv.setText(textVal);
           }
    });
    return view;
}