Android Diplay Fragment下面的Activity

时间:2017-01-13 18:41:20

标签: android android-layout android-fragments

我是Android新手并学习如何使用片段。 我遇到过这个问题,我在活动中有EditTextButton

单击按钮,我想在TextView中显示EditText的内容,这是Fragment的一部分。所以我的布局应该是EditTextButton,而不是TextView

我的代码正常运行,但我无法在按钮下方显示TextView,它出现在屏幕顶部。

主要活动xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.dhaval.fragmentassignment6a.MainActivity">


    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"

        android:ems="10"
        android:layout_marginTop="24dp"
        android:id="@+id/editText"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:singleLine="false" />

    <Button
        android:text="Submit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="33dp"
        android:id="@+id/button"
        android:layout_below="@+id/editText"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <FrameLayout
        android:id="@id/fragmentSingle"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        tools:layout_marginTop="150sp">

    </FrameLayout>
</RelativeLayout>

片段xml:

    <FrameLayout 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.dhaval.fragmentassignment6a.SimpleAddition">

        <!-- TODO: Update blank fragment layout -->
        <TextView
            android:id="@id/textView12"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="@string/hello_blank_fragment"
            android:textColor="@color/colorAccent" />

    </FrameLayout>

MainActivity.java

   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        edittext=(EditText) findViewById(R.id.editText);
        btn=(Button)findViewById(R.id.button);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle=new Bundle();
                bundle.putString("edittext1", String.valueOf(edittext.getText()));
                FragmentManager fragmentManager=getSupportFragmentManager();
                FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
                SimpleAddition simpleAddition=new SimpleAddition();
                simpleAddition.setArguments(bundle);
                fragmentTransaction.add(R.id.fragmentSingle,simpleAddition,"bottom");
                fragmentTransaction.commit();

            }
        });
    }

1 个答案:

答案 0 :(得分:0)

您必须更改MainActivity.xml和fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:ems="10"
    android:layout_marginTop="24dp"
    android:id="@+id/editText"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:singleLine="false" />

<Button
    android:text="Submit"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="33dp"
    android:id="@+id/button"
    android:layout_below="@+id/editText"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<FrameLayout
    android:id="@+id/fragmentSingle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"  <----made changes here---->
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"     
    >

   </FrameLayout>
</RelativeLayout>

MainActivity.xml

<FrameLayout 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="---same of yours-----">

<!-- TODO: Update blank fragment layout -->
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"    <''''done changes here''''''>
    android:text="@string/hello_blank_fragment"
    android:id="@+id/tv"/>

fragment.xml之