代码编织梦想

Activity->Fragment

在Activity中创建Bundle数据包,并调用Fragment的setArguments(Bundle bundle)方法

package com.example.fragment;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
    private RadioGroup group;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        group = (RadioGroup) findViewById(R.id.RadioGroup);
        group.setOnCheckedChangeListener(this);


    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.first:
                Intent intent = new Intent(this, MainActivity2.class);
                startActivity(intent);

                break;
            case R.id.second: {
                MyFragment2 fragment2 = new MyFragment2();
                FragmentManager fragmentManager = getFragmentManager();
                //获取开启事务的对象
                FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
                //第一个参数:要把fragment加载到哪个布局文件 第二个参数为fragment对象
                beginTransaction.add(R.id.fragment, fragment2);
                //通过物理返回可以回退到上个状态
                beginTransaction.addToBackStack(null);
                //事务提交
                beginTransaction.commit();
                break;


            }
            case R.id.third: {

                break;


            }
            case R.id.fourth: {
                Intent intent2=new Intent(this,MainActivity4.class);
                startActivity(intent2);
                break;


            }
        }
    }
}

package com.example.fragment;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import javax.microedition.khronos.egl.EGLDisplay;

public class MainActivity4 extends Activity {
    private EditText editText;
    private Button send;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main4);
        editText = findViewById(R.id.editText);
        send = findViewById(R.id.send);
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = editText.getText().toString();
                MyFragment5 myFragment5 = new MyFragment5();
                Bundle bundle = new Bundle();
                //传递字符串类型的字,key=name
                bundle.putString("name",text);
                myFragment5.setArguments(bundle);
                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
                //动态加载时设置fragment的tag
                beginTransaction.add(R.id.layout,myFragment5,"fragment");
                beginTransaction.commit();
                Toast.makeText(MainActivity4.this,"向Fragment发送数据"+text,Toast.LENGTH_SHORT).show();
            }
        });



    }



}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/layout">

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发送"
        android:id="@+id/send"/>


</LinearLayout>
package com.example.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import org.w3c.dom.Text;

public class MyFragment5 extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment2,container,false);
        TextView tv = view.findViewById(R.id.text);
        //接收端获取数据包
        String text = getArguments().get("name")+"";
        tv.setText(text);
        Toast.makeText(getActivity(),"已成功接收到"+text,Toast.LENGTH_SHORT).show();

        return view;
    }
}

Fragment->Activity

在Fragment中定义一个内部回调接口,再让包含该Fragment的Activity实现该回调接口

动态加载Fragment

package com.example.fragment;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
    private RadioGroup group;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        group = (RadioGroup) findViewById(R.id.RadioGroup);
        group.setOnCheckedChangeListener(this);


    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.first:
                Intent intent = new Intent(this, MainActivity2.class);
                startActivity(intent);

                break;
            case R.id.second: {
                MyFragment2 fragment2 = new MyFragment2();
                FragmentManager fragmentManager = getFragmentManager();
                //获取开启事务的对象
                FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
                //第一个参数:要把fragment加载到哪个布局文件 第二个参数为fragment对象
                beginTransaction.add(R.id.fragment, fragment2);
                //通过物理返回可以回退到上个状态
                beginTransaction.addToBackStack(null);
                //事务提交
                beginTransaction.commit();
                break;


            }
            case R.id.third: {

                break;


            }
            case R.id.fourth: {
                Intent intent2=new Intent(this,MainActivity4.class);
                startActivity(intent2);
                break;


            }
        }
    }
}

package com.example.fragment;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import javax.microedition.khronos.egl.EGLDisplay;

public class MainActivity4 extends Activity implements MyFragment5.MyListener {
    private EditText editText;
    private Button send;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main4);
        editText = findViewById(R.id.editText);
        send = findViewById(R.id.send);
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = editText.getText().toString();
                MyFragment5 myFragment5 = new MyFragment5();
                Bundle bundle = new Bundle();
                //传递字符串类型的字,key=name
                bundle.putString("name",text);
                myFragment5.setArguments(bundle);
                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
                //动态加载时设置fragment的tag
                beginTransaction.add(R.id.layout,myFragment5,"fragment");
                beginTransaction.commit();
                Toast.makeText(MainActivity4.this,"向Fragment发送数据"+text,Toast.LENGTH_SHORT).show();
            }
        });



    }


    @Override
    public void thank(String code) {
        Toast.makeText(MainActivity4.this,"已成功接收到"+code+"客气了!",Toast.LENGTH_SHORT).show();
    }
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/layout">

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发送"
        android:id="@+id/send"/>


</LinearLayout>
package com.example.fragment;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import org.w3c.dom.Text;

public class MyFragment5 extends Fragment {
    private String code = "Thank you! Activity";
    public MyListener myListener;

    public interface MyListener{
        public void thank(String code);
    }
    //当Fragment添加到Activity中时调用这个方法,从而使得Fragment获取了当前的Activity,也就是需要发送回数据的Activity
    @Override
    public void onAttach(Activity activity) {
        myListener = (MyListener) activity;
        super.onAttach(activity);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment2,container,false);
        TextView tv = view.findViewById(R.id.text);
        //接收端获取数据包
        String text = getArguments().get("name")+"";
        tv.setText(text);
        Toast.makeText(getActivity(),"已成功接收到"+text,Toast.LENGTH_SHORT).show();
        Toast.makeText(getActivity(),"向Activity发送"+code,Toast.LENGTH_SHORT).show();

        //从而调用MainActivity4中的thank方法
        myListener.thank(code);
        return view;
    }
}

静态加载Fragment

package com.example.fragment;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
    private RadioGroup group;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        group = (RadioGroup) findViewById(R.id.RadioGroup);
        group.setOnCheckedChangeListener(this);


    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.first:
                Intent intent = new Intent(this, MainActivity2.class);
                startActivity(intent);

                break;
            case R.id.second: {
                MyFragment2 fragment2 = new MyFragment2();
                FragmentManager fragmentManager = getFragmentManager();
                //获取开启事务的对象
                FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
                //第一个参数:要把fragment加载到哪个布局文件 第二个参数为fragment对象
                beginTransaction.add(R.id.fragment, fragment2);
                //通过物理返回可以回退到上个状态
                beginTransaction.addToBackStack(null);
                //事务提交
                beginTransaction.commit();
                break;


            }
            case R.id.third: {

                break;


            }
            case R.id.fourth: {
                Intent intent2=new Intent(this,MainActivity4.class);
                startActivity(intent2);
                break;


            }
        }
    }
}

package com.example.fragment;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import javax.microedition.khronos.egl.EGLDisplay;

public class MainActivity4 extends Activity implements MyFragment5.MyListener {
    private EditText editText;
    private Button send;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main4);
        editText = findViewById(R.id.editText);
        send = findViewById(R.id.send);
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = editText.getText().toString();
                MyFragment5 myFragment5 = new MyFragment5();
                Bundle bundle = new Bundle();
                //传递字符串类型的字,key=name
                bundle.putString("name",text);
                myFragment5.setArguments(bundle);
                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
                //动态加载时设置fragment的tag
                beginTransaction.add(R.id.layout,myFragment5,"fragment");
                beginTransaction.commit();
                Toast.makeText(MainActivity4.this,"向Fragment发送数据"+text,Toast.LENGTH_SHORT).show();
            }
        });

        
        FragmentManager fragmentManager = getFragmentManager();
        //获取了静态加载的Fragment对象
        Fragment findFragmentById = fragmentManager.findFragmentById(R.id.frag);
        //进行类型转换
        MyFragment frag =(MyFragment)findFragmentById;
        frag.setBbb("fragment静态传值");




    }


    @Override
    public void thank(String code) {
        Toast.makeText(MainActivity4.this,"已成功接收到"+code+"客气了!",Toast.LENGTH_SHORT).show();
    }
}

main4.xml 含有加载的静态fragment

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/layout">

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发送"
        android:id="@+id/send"/>
    <fragment
        android:id="@+id/frag"
        android:name="com.example.fragment.MyFragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>


</LinearLayout>
package com.example.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

public class MyFragment extends Fragment {
    //设置需要传递的内容
    private String bbb;

    //设置对应的get,set方法
    public String getBbb() {
        return bbb;
    }

    public void setBbb(String bbb) {
        this.bbb = bbb;
    }



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

        //将Layout布局文件转换为View对象
        /*
        * resource:需要加载的布局文件
        * root:加载Layout的父ViewGroup
        * attactToRoot:false,不返回父ViewGroup
        * */
        View view = inflater.inflate(R.layout.fragment,container,false);
        TextView text = view.findViewById(R.id.text);

        Button button = view.findViewById(R.id.Button);
        text.setText("静态加载Fragment");
        button.setText("获取内容");
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String value = getBbb();
                Toast.makeText(getActivity(),"value="+value,Toast.LENGTH_SHORT).show();

            }
        });
        return view;

    }
}

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/m0_66944345/article/details/131027333

android 12.0launcher3 禁止首屏时钟部件拖动到其他屏-爱代码爱编程

1.概述   在12.0的系统rom定制化开发中, 在系统Launcher3中,首页中间默认有个时钟部件来显示时间,并且可以任意拖拽到其他地方,如果拖动到其他屏显的很不美观,所以根据需要要求时钟部件 不能拖拽到其他屏,所以就要从拖拽开始处理,判断如果是时钟部件,就不让拖拽到其他屏,先从拖拽流程分析 2.Launcher3 禁止首屏时钟部件拖动到其他屏的

android 12.0launcher3禁止拖动图标到hotseat-爱代码爱编程

1.概述 在12.0系统Launcher3进行定制化开发中,对于hotseat的开发中,由功能需求要求禁止拖动图标到Hotseat的功能,而拖拽也是在workspace.java中处理的 接下来就从workspace.java 开始找解决的办法 2.Launcher3禁止拖动图标到Hotseat相关代码分析 packages/apps/Launche

wav 格式和音频裁剪、转码处理_wav格式的音频剪切-爱代码爱编程

文章目录 0、参考资料1、WAV 格式了解1.1 WAV 文件头1.2 RIFF Chunk 区块1.3 Format Chunk 区块1.4 Data Chunk 区块 2、音频剪裁 -> 解码