简体   繁体   English

如何将数据从底表传递到Android中的片段?

[英]How to pass data from bottom sheet to a fragment in android?

I am trying to pass values from a bottom sheet which contains some TextViews to a fragment. 我试图将值从包含一些TextViews的底部工作表传递到片段。 My fragment contains an EditText field and a Floating action button.When the user clicks on the floating action button, the bottom sheet shows up which has a number of TextViews, when the user clicks on any of the textViews on the bottom sheet, the value or the string of that TextView should be displayed in the editText field of the fragment and the bottom sheet should be dismissed. 我的片段包含一个EditText字段和一个Floating action按钮。当用户单击浮动动作按钮时,底部工作表显示出来,其中包含多个TextViews,当用户单击底部工作表中的任何textViews时,该值或应在片段的editText字段中显示该TextView的字符串,并关闭底部的工作表。

I have tried implementing the onClickListener inside the setOnShowListener method but it doesn't seem to work. 我已经尝试在setOnShowListener方法内实现onClickListener,但是它似乎不起作用。

Here is my code: 这是我的代码:

Fragment_TextPropert1_EditText.xml Fragment_TextPropert1_EditText.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.sumitroy.TextProperty1_EditText"
    >
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:cardCornerRadius="15dp"
        android:padding="15dp"
        android:layout_below="@+id/view"
        android:layout_marginTop="10dp"
        app:cardUseCompatPadding="true"
        android:id="@+id/view2">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textMultiLine"
            android:hint="Enter Your Comments Here.."
            android:id="@+id/userText"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true" />

    </android.support.v7.widget.CardView>

<ImageButton
      android:layout_width="65dp"
      android:layout_height="65dp"
      android:id="@+id/example_Ads"
      android:background="@drawable/oval"
      android:layout_alignParentBottom="true"
      android:layout_alignParentEnd="true"
      android:layout_marginBottom="20dp"
      android:layout_marginRight="15dp"
      android:src="@drawable/double_plus"
     />


</RelativeLayout>

TextProperty1_bottomsheet.xml TextProperty1_bottomsheet.xml

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

<RelativeLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior">


    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:cardCornerRadius="15dp"
    android:padding="15dp"
    android:layout_marginTop="15dp"
    app:cardUseCompatPadding="true"
    android:id="@+id/view1">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Comment1"
        android:id="@+id/example_Ad1"
        android:padding="10dp"
        android:textStyle="bold"
        android:textSize="14sp"
        android:layout_below="@+id/view"
        android:layout_alignParentStart="true" />

</android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:cardCornerRadius="15dp"
        android:padding="15dp"
        android:layout_marginTop="10dp"
        app:cardUseCompatPadding="true"
        android:layout_below="@+id/view1"
        android:id="@+id/view2">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Comment 2"
            android:id="@+id/example_Ad2"
            android:padding="10dp"
            android:textStyle="bold"
            android:textSize="14sp"
            android:layout_below="@+id/view"
             />

    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:cardCornerRadius="15dp"
        android:padding="15dp"
        android:layout_marginTop="15dp"
        app:cardUseCompatPadding="true"
        android:id="@+id/view3"
        android:layout_below="@+id/view2">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Comment 3 "
            android:id="@+id/example_Ad3"
            android:padding="10dp"
            android:textStyle="bold"
            android:textSize="14sp"
            android:layout_below="@+id/view"
            />

    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:cardCornerRadius="15dp"
        android:padding="15dp"
        android:layout_marginTop="15dp"
        app:cardUseCompatPadding="true"
        android:id="@+id/view4"
        android:layout_below="@+id/view3">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Comment 4 "
            android:id="@+id/example_Ad4"
            android:padding="10dp"
            android:textStyle="bold"
            android:textSize="14sp"
            android:layout_below="@+id/view"
             />

    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:cardCornerRadius="15dp"
        android:padding="15dp"
        android:layout_marginTop="15dp"
        app:cardUseCompatPadding="true"
        android:id="@+id/view5"
        android:layout_below="@+id/view4">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Comment 5 "
            android:id="@+id/example_Ad5"
            android:padding="10dp"
            android:textStyle="bold"
            android:textSize="14sp"
            android:layout_below="@+id/view"
             />

    </android.support.v7.widget.CardView>


</RelativeLayout>
</ScrollView>

TextProperty1_EditText.java TextProperty1_EditText.java

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.BottomSheetDialog;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;


/**
 * A simple {@link Fragment} subclass.
 */
public class TextProperty1_EditText extends Fragment {

    View bottomSheetView;
    EditText editText1;
    TextView t1;
    BottomSheetDialog bottomSheetDialog;
    BottomSheetBehavior bottomSheetBehavior;
    ImageButton floatButton;
    RelativeLayout backgroundLayout;


    public TextProperty1_EditText() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        final View rootview= inflater.inflate(R.layout.fragment_text_property1__edit_text, container, false);
        editText1=(EditText) rootview.findViewById(R.id.userText);
        floatButton=(ImageButton)rootview.findViewById(R.id.example_Ads);
        floatButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //Toast.makeText(v.getContext(),"Floating Button Works",Toast.LENGTH_SHORT).show();
                bottomSheetView=getActivity().getLayoutInflater().inflate(R.layout.textproperty1_bottomsheet,null);
                bottomSheetDialog=new BottomSheetDialog(rootview.getContext());
                bottomSheetDialog.setContentView(bottomSheetView);
                bottomSheetBehavior=BottomSheetBehavior.from((View) bottomSheetView.getParent());
                bottomSheetDialog.show();

                bottomSheetDialog.setOnShowListener(new DialogInterface.OnShowListener() {
                    @Override
                    public void onShow(DialogInterface dialog) {
                       t1=(TextView)bottomSheetView.findViewById(R.id.example_Ad1);
                        t1.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                String take1=t1.getText().toString();
                                //Toast.makeText(bottomSheetView.getContext(),"Floating Button Works",Toast.LENGTH_SHORT).show();
                                editText1.setText(take1);
                                bottomSheetDialog.dismiss();
                            }
                        });



                    }
                });
                bottomSheetDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {

                    }
                });

                bottomSheetDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
                    @Override
                    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                        //Toast.makeText(bottomSheetView.getContext(),"Floating Button Works",Toast.LENGTH_SHORT).show();
                        return false;
                    }
                });

            }
        });


        return rootview;
    }

}

As a simple solution you may use LocalBroadcastManager . 作为一个简单的解决方案,您可以使用LocalBroadcastManager

public static final String SOME_INTENT_FILTER_NAME = "SOME_INTENT_FILTER_NAME";

In your fragment: 在您的片段中:

private BroadcastReceiver someBroadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        //TODO extract extras from intent
    }
};
@Override
public void onResume() {
    super.onResume();
    LocalBroadcastManager.getInstance(getContext()).registerReceiver(someBroadcastReceiver,
            new IntentFilter(SOME_INTENT_FILTER_NAME));
}
@Override
public void onPause() {
    LocalBroadcastManager.getInstance(getContext()).unregisterReceiver(someBroadcastReceiver);
    super.onPause();
}

In your bottomsheet: 在您的底表中:

Intent someIntent = new Intent(SOME_INTENT_FILTER_NAME);
//TODO put extras to your intent
LocalBroadcastManager.getInstance(context).sendBroadcast(someIntent);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将数据从片段传递到android中的底部工作表? - How to pass the data from the fragment to bottom sheet in android? 如何将数据从适配器 class 传递到底部工作表对话框片段 - How to pass data from adapter class to bottom sheet dialog fragment 将数据从底部工作表对话框片段传递到片段 - Pass data from Bottom Sheet Dialog Fragment to Fragment 如何从底部工作表片段开始活动 - How to start activity from bottom sheet fragment 如何在从下到上滑动时打开底部片断 android 就像 snapchat 故事 - how to open bottom sheet fragment while sliding from bottom to top android like snapchat stories 如何将数据从活动传递到Android上的片段 - How to pass data from an activity to a fragment on android 如何将数据从片段传递到另一个 - android - How to pass data from fragment to another - android 如何从底部工作表导航片段移动到其他片段 - How to move to other fragment from Bottom Sheet Navigation Fragment 片段中的android底部工作表未将数据绑定到回收站视图 - android bottom sheet in fragment not binding data to recycler view 如何使用 java 将数据从片段传递到 android 中的另一个片段? - How to pass data from fragment to another fragment in android using java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM