Day05

作者: 鴻9527 | 来源:发表于2019-07-10 21:23 被阅读0次

    Day05

    fragment传值 方法一

    fragment布局一

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <Button
            android:id="@+id/button_one"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="25sp"
            android:text="发送"
            android:layout_centerInParent="true"/>
    
    </RelativeLayout>
    

    acticity布局

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <RelativeLayout
            android:id="@+id/rela"
            android:layout_width="match_parent"
            android:layout_height="300dp">
    
        </RelativeLayout>
    
        <Button
            android:id="@+id/button_show"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="show"
            android:textSize="25dp"
            android:layout_centerInParent="true"
            android:onClick="show"/>
    
        <Button
            android:id="@+id/button_cz"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="接收"
            android:textSize="25sp"
            android:layout_centerHorizontal="true"
            android:layout_below="@id/button_show"
            android:layout_marginTop="10dp"/>
    
    </RelativeLayout>
    

    java代码

    Fragment1 java代码

    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    
    import androidx.annotation.NonNull;
    import androidx.annotation.Nullable;
    import androidx.fragment.app.Fragment;
    
    public class Fragment1 extends Fragment {
    
        private Handler handler;
    
        public Fragment1(Handler handler) {
            this.handler = handler;
        }
    
        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View inflate = inflater.inflate(R.layout.fragment1, container, false);
            inflate.findViewById(R.id.button_one).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Message message = new Message();
                    message.obj = "我是发送的消息";
                    message.what = 1;
                    handler.sendMessage(message);
                }
            });
            return inflate;
        }
    }
    

    MainActivity java代码

    import androidx.annotation.NonNull;
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.fragment.app.FragmentManager;
    import androidx.fragment.app.FragmentTransaction;
    
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.view.View;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity {
    
        Handler handler = new Handler(){
            @Override
            public void handleMessage(@NonNull Message msg) {
                super.handleMessage(msg);
    
                switch (msg.what){
    
                    case 1:
                        String s = msg.obj.toString();
                        Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
                        break;
    
                }
    
            }
        };
    
        private Fragment1 fragment1 = new Fragment1(handler);
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    
        public void show(View view) {
    
            FragmentManager manager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = manager.beginTransaction();
            fragmentTransaction.add(R.id.rela,fragment1,"啊啊啊");
            fragmentTransaction.commit();
    
        }
    }
    

    效果图

    bb.gif

    fragment传值 方法二

    fragment布局一

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <Button
            android:id="@+id/button_one"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="25sp"
            android:text="发送"
            android:layout_centerInParent="true"/>
    
    </RelativeLayout>
    

    acticity布局

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <RelativeLayout
            android:id="@+id/rela"
            android:layout_width="match_parent"
            android:layout_height="300dp">
    
        </RelativeLayout>
    
        <Button
            android:id="@+id/button_show"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="show"
            android:textSize="25dp"
            android:layout_centerInParent="true"
            android:onClick="show"/>
    
        <Button
            android:id="@+id/button_cz"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="接收"
            android:textSize="25sp"
            android:layout_centerHorizontal="true"
            android:layout_below="@id/button_show"
            android:layout_marginTop="10dp"
            android:onClick="jieshou"/>
    </RelativeLayout>
    

    java代码

    Fragment1 java代码

    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    
    import androidx.annotation.NonNull;
    import androidx.annotation.Nullable;
    import androidx.fragment.app.Fragment;
    
    public class Fragment1 extends Fragment {
    
        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View inflate = inflater.inflate(R.layout.fragment1, container, false);
            inflate.findViewById(R.id.button_one).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Bundle bundle = new Bundle();
                    bundle.putString("key","我是发送的数据");
                    Fragment1.this.setArguments(bundle);
                }
            });
            return inflate;
        }
    }
    

    MainActivity java代码

    import androidx.appcompat.app.AppCompatActivity;
    import androidx.fragment.app.FragmentManager;
    import androidx.fragment.app.FragmentTransaction;
    
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity {
    
        private Fragment1 fragment1 = new Fragment1();
        private FragmentManager manager;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            manager = getSupportFragmentManager();
        }
    
        public void show(View view) {
    
            FragmentTransaction fragmentTransaction = manager.beginTransaction();
            fragmentTransaction.add(R.id.rela,fragment1,"abc");
            fragmentTransaction.commit();
    
        }
    
        public void jieshou(View view) {
    
            Bundle arguments = fragment1.getArguments();
            String key = arguments.getString("key");
            if (key != null){
                Toast.makeText(this, key, Toast.LENGTH_SHORT).show();
            }
    
        }
    }
    
    

    效果图

    aa.gif

    相关文章

      网友评论

          本文标题:Day05

          本文链接:https://www.haomeiwen.com/subject/qqikkctx.html