动态地在单个活动中实现多个片段

smriti3:

我正在研究片段


我正在尝试实现的用例 ::

  • 我正在使用动态片段
  • 我在一个活动中使用了三个片段
  • 我的目标是在所有三个片段之间进行交流
  • 我正在使用支持包中的片段

每个片段都有一个小部件

  • my_fragment1edittext
  • my_fragment2button
  • my_fragment3TextView

上点击button从文本edittext必须显示在textview


到目前为止,我已经尝试了以下大多数情况


Top_Fragment.java

public class Top_Fragment extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        View view=inflater.inflate(R.layout.my_fragment1, container, false);

        return view;
    }
}

Middle_Fragment.java

package com.example.deleteme;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

public class Middle_Fragment extends Fragment{

    View view;
    Button btn;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        view=inflater.inflate(R.layout.my_fragment2, container, false);
        btn=(Button) view.findViewById(R.id.button1);
        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub



            }
        });
        return view;
    }
}

Bottom_Fragment.java

public class Bottom_Fragment extends Fragment{

    View view;
    TextView display_text;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        view=inflater.inflate(R.layout.my_fragment3, container,false);
        display_text=(TextView) view.findViewById(R.id.editText1);
        return view;
    }

    public void setName(String Name){
        display_text.setText("Result::" + Name);
    }



}

MainActivity.java

public class MainActivity extends FragmentActivity {

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

        Top_Fragment frg=new Top_Fragment();//create the fragment instance for the top fragment
        Middle_Fragment frg1=new Middle_Fragment();//create the fragment instance for the middle fragment
        Bottom_Fragment frg2=new Bottom_Fragment();//create the fragment instance for the bottom fragment

        FragmentManager manager=getSupportFragmentManager();//create an instance of fragment manager

        FragmentTransaction transaction=manager.beginTransaction();//create an instance of Fragment-transaction

        transaction.add(R.id.My_Container_1_ID, frg, "Frag_Top_tag");
        transaction.add(R.id.My_Container_2_ID, frg1, "Frag_Middle_tag");
        transaction.add(R.id.My_Container_3_ID, frg2, "Frag_Bottom_tag");


        transaction.commit();

    }


}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".MainActivity" 
    android:background="@color/black">

    <FrameLayout
        android:id="@+id/My_Container_1_ID"
        android:layout_width="fill_parent"
        android:layout_height="150dp" 
        android:background="@color/yellow">
    </FrameLayout>

    <FrameLayout
        android:id="@+id/My_Container_2_ID"
        android:layout_width="fill_parent"
        android:layout_height="150dp"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/My_Container_1_ID"
        android:background="@color/Orange" >
    </FrameLayout>

    <FrameLayout
        android:id="@+id/My_Container_3_ID"
        android:layout_width="fill_parent"
        android:layout_height="150dp"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/My_Container_2_ID"
        android:background="@color/purple" >
    </FrameLayout>

</RelativeLayout>

my_fragment1.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/green" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:ems="10"
        android:textColor="#000000"
        android:singleLine="true" >

        <requestFocus />
    </EditText>

</RelativeLayout>

my_fragment2.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:background="@color/pink">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="@color/black"
        android:text="Button"
        android:textColor="#FFFFFF" />

</RelativeLayout>

my_fragment3.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="TextView"
        android:textColor="#000000"
        android:textSize="30dp" />

</RelativeLayout>

我的输出如下

在此处输入图片说明


我在实现 :: 时遇到问题

  • 我不能够设置从获得的值edit texttextview上的点击button

有任何想法吗?

拉贡南丹:

所有片段到片段的通信都是通过关联的活动完成的。两个片段永远不要直接通信。

http://developer.android.com/training/basics/fragments/communicating.html

test.java //在您的情况下 MainActivity

public class test extends FragmentActivity implements textEntered {

    String value;
    boolean check = false;
    BottomFragment frg2;
    FragmentTransaction transaction;

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

        Top_Fragment frg = new Top_Fragment();
        frg2 = new BottomFragment();

        FragmentManager manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();
        transaction.add(R.id.My_Container_1_ID, frg, "Frag_Top_tag");
        transaction.add(R.id.My_Container_3_ID, frg2, "Frag_Bottom_tag");
        transaction.commit();
    }

    @Override
    public void setValue(String editextvalue) {
        value = editextvalue;
        if (frg2 != null) {
            frg2.setName(value);
        } else {
            Toast.makeText(getApplicationContext(), "fragment 2  is null", 1000).show();
        }
    }

}    

Top_Fragment.java

public class Top_Fragment extends Fragment {
    textEntered mCallback;
    Button b;
    EditText ed;

    public interface textEntered {
        public void setValue(String editextvalue);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        View view = inflater.inflate(R.layout.my_fragment1, container, false);
        ed = (EditText) view.findViewById(R.id.editText1);


        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        b = (Button) getView().findViewById(R.id.button1);
        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String s = ed.getText().toString();
                mCallback.setValue(s);
            }

        });
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception
        try {
            mCallback = (textEntered) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() +
                " must implement textEntered");
        }
    }
}

my_fragment1.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:ems="10"
        android:textColor="#000000"
        android:singleLine="true" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="Button" />

</RelativeLayout>

改成

 display_text=(TextView) view.findViewById(R.id.textView1);
 // id is textView 1 not editText1

在BottomFragment

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

动态地在FrameLayout中添加多个片段

如何动态地从Firestore阵列中删除多个元素?

在Spark Dataframe中动态地在多个列上枢轴

如何动态地实现特定功能?

多个片段到单个活动

如何动态地将Cover div包装在reactjs片段中

在recyclerview中动态地将项目添加到列表中并复制到片段中的列表中

在C ++中,如何动态地从文件中创建一个类的多个对象?

在Android中动态地将多个WebView添加到鳍状肢

如何动态地使用虚拟方法实现类

动态地在Haskell类的实现之间交换

用户在运行时动态地输入多个

动态地将多个选择的值相加并计算总和

从工具栏隐藏元素,在单个活动中实现,在各种片段上

如何动态地将片段添加到现有片段?

如何动态地了解和实例化仅在Python模块中实现的一个类

动态地将EditText与(如果可能)具有字符串ID的片段一起添加到片段中

如何在具有单个活动和多个片段的 ClientServer 架构中更新 Android 中的 RecyclerViews

单个活动中的多个WebView

单个活动中的多个意图

如何动态地将ImageButtons和OnClick侦听器添加到300多个图像中?

如何在HTML页面上实时动态地显示多个行框的标题框中的文本?

如何动态地(通过使用通配符)对数组文字中的多个制表符范围进行分组?

如何在Java中动态地将一个涟漪可绘制对象应用于多个按钮?

在Android片段活动中实现后退按钮

多个活动和片段中的同一Spinner。哪种是最好的实现方式?

如何动态地设置网络中边缘的大小+ python

动态地在dict中设置一个值

在fullpage.js中动态地使元素固定(标题)