用户登录
用户注册

分享至

2020-10-10

  • 作者: 亖呉?盀
  • 来源: 51数据库
  • 2021-08-20

刚学习AndroidStudio,我们先写一个xml界面,然后再实现它的功能

现在我们先实现一个点击切换标题的功能

在activity_main.xml里面写入以下代码,将按钮以及排版设置好

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

    <TextView
        android:id="@+id/tv_title"
        android:textSize="36sp"
        android:text="标题"
        android:gravity="center"
        android:textColor="@android:color/white"
        android:background="@color/colorPrimary"
        android:layout_width="match_parent"
        android:layout_height="56dp"/>

    <LinearLayout
        android:id="@+id/main_body"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="vertical">

    </LinearLayout>


        <LinearLayout
            android:id="@+id/bottom_bar"
            android:layout_alignParentBottom="true"
            android:orientation="horizontal"
            android:background="#F2F2F8"
            android:layout_width="match_parent"
            android:layout_height="50dp">
            <RelativeLayout
                android:id="@+id/bottom_bar_1_btn"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:onClick="onClick"
                android:layout_height="match_parent">
                <TextView
                    android:id="@+id/bottom_bar_text_1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_centerHorizontal="true"
                    android:layout_marginBottom="2dp"
                    android:gravity="center"
                    android:text="课程"
                    android:textColor="#000000"
                    android:textSize="14sp"/>

                <ImageView
                    android:id="@+id/bottom_bar_image_1"
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:layout_above="@+id/bottom_bar_text_1"
                    android:layout_alignParentTop="true"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="0dp"
                    android:layout_marginBottom="2dp"
                    android:src="@mipmap/ic_course" />
            </RelativeLayout>
            <RelativeLayout
                android:id="@+id/bottom_bar_2_btn"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:onClick="onClick"
                android:layout_height="match_parent">

                <TextView
                    android:id="@+id/bottom_bar_text_2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_centerHorizontal="true"
                    android:layout_marginBottom="2dp"
                    android:gravity="center"
                    android:text="习题"
                    android:textColor="#000000"
                    android:textSize="14sp" />

                <ImageView
                    android:id="@+id/bottom_bar_image_2"
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:layout_alignParentTop="true"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="2dp"
                    android:src="@mipmap/ic_exercise" />
            </RelativeLayout>
            <RelativeLayout
                android:id="@+id/bottom_bar_3_btn"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:onClick="onClick"
                android:layout_height="match_parent">
                <TextView
                    android:id="@+id/bottom_bar_text_3"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_centerHorizontal="true"
                    android:layout_marginBottom="2dp"
                    android:gravity="center"
                    android:text="搜索"
                    android:textColor="#000000"
                    android:textSize="14sp"/>
                <ImageView
                    android:id="@+id/bottom_bar_image_3"
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:layout_above="@+id/bottom_bar_text_3"
                    android:layout_alignParentTop="true"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="2dp"
                    android:src="@mipmap/ic_message"/>
            </RelativeLayout>
            <RelativeLayout
                android:id="@+id/bottom_bar_4_btn"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:onClick="onClick"
                android:layout_height="match_parent">
                <TextView
                    android:id="@+id/bottom_bar_text_4"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_centerHorizontal="true"
                    android:layout_marginBottom="2dp"
                    android:gravity="center"
                    android:text="我"
                    android:textColor="#000000"
                    android:textSize="14sp"/>
                <ImageView
                    android:id="@+id/bottom_bar_image_4"
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:layout_above="@+id/bottom_bar_text_4"
                    android:layout_alignParentTop="true"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="2dp"
                    android:src="@mipmap/ic_my"/>
            </RelativeLayout>
        </LinearLayout>


</LinearLayout>

写完以上的代码,我们会得到这样一个界面

然后我们新建一个fragment

我们给它起个名字testFragment
然后我们就得到一个fragment_test.xml
我们在里面添加一个id android:id="@+id/tv_content" 这样方便我们后面功能的实现

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


        initView();
        initFragment();

        replaceFragment(fragments.get(R.id.bottom_bar_2_btn));


    }
    //实现回调接口
    @Override
    public void setTitle(String title) {
        tvTitle.setText(title);
    }

我们再onCreate方法里面添加两个构造函数,接下来我们实现这几个函数

private void initFragment() {
        fragments = new SparseArray<>();
        fragments.put(R.id.bottom_bar_1_btn,testFragment.newInstance("课程"));
        fragments.put(R.id.bottom_bar_2_btn,testFragment.newInstance("习题"));
        fragments.put(R.id.bottom_bar_3_btn,testFragment.newInstance("搜索"));
        fragments.put(R.id.bottom_bar_4_btn,testFragment.newInstance("我"));

    }
    private void replaceFragment(Fragment fragment){
        FragmentManager manager=getSupportFragmentManager();
        FragmentTransaction transaction=manager.beginTransaction();
        transaction.replace(R.id.main_body,fragment);
        transaction.commit();
    }

    private void initView(){
        tvTitle = findViewById(R.id.tv_title);

        bottom_bar_text_1=findViewById(R.id.bottom_bar_text_1);
        bottom_bar_text_2=findViewById(R.id.bottom_bar_text_2);
        bottom_bar_text_3=findViewById(R.id.bottom_bar_text_3);
        bottom_bar_text_4=findViewById(R.id.bottom_bar_text_4);
        bottom_bar_image_1=findViewById(R.id.bottom_bar_image_1);
        bottom_bar_image_2=findViewById(R.id.bottom_bar_image_2);
        bottom_bar_image_3=findViewById(R.id.bottom_bar_image_3);
        bottom_bar_image_4=findViewById(R.id.bottom_bar_image_4);

        mian_body_bar=findViewById(R.id.main_body_bar);
        bottom_bar_1_btn=findViewById(R.id.bottom_bar_1_btn);
        bottom_bar_2_btn=findViewById(R.id.bottom_bar_2_btn);
        bottom_bar_3_btn=findViewById(R.id.bottom_bar_3_btn);
        bottom_bar_4_btn=findViewById(R.id.bottom_bar_4_btn);

        bottom_bar_1_btn.setOnClickListener(this);
        bottom_bar_2_btn.setOnClickListener(this);
        bottom_bar_3_btn.setOnClickListener(this);
        bottom_bar_4_btn.setOnClickListener(this);

        setSelectStatus(1);//默认选择
    }

在onclick函数中获取事件

 @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.bottom_bar_1_btn:
                setSelectStatus(0);
                break;
            case R.id.bottom_bar_2_btn:
                setSelectStatus(1);
                break;
            case R.id.bottom_bar_3_btn:
                setSelectStatus(2);
                break;
            case R.id.bottom_bar_4_btn:
                setSelectStatus(3);
                break;
        }
        replaceFragment(fragments.get(view.getId()));
    }

点击后变色并跳转

 private  void setSelectStatus(int index){
        switch(index){
            case 0:
                //图片点击选择变换图片,颜色的改变,其他变为原来的颜色,并保持原来的图片
                bottom_bar_image_1.setImageResource(R.mipmap.ic_course_selected);
                bottom_bar_text_1.setTextColor(Color.parseColor("#0097F7"));

                //其他文本和颜色不变
                bottom_bar_image_2.setImageResource(R.mipmap.ic_exercise);
                bottom_bar_text_2.setTextColor(Color.parseColor("#000000"));

                bottom_bar_image_3.setImageResource(R.mipmap.ic_message);
                bottom_bar_text_3.setTextColor(Color.parseColor("#000000"));

                bottom_bar_image_4.setImageResource(R.mipmap.ic_my);
                bottom_bar_text_4.setTextColor(Color.parseColor("#000000"));

                break;


            case 1:
                //图片点击选择变换图片,颜色的改变,其他变为原来的颜色,并保持原来的图片
                bottom_bar_image_2.setImageResource(R.mipmap.ic_exercise_selected);
                bottom_bar_text_2.setTextColor(Color.parseColor("#0097F7"));

                //其他文本和颜色不变
                bottom_bar_image_1.setImageResource(R.mipmap.ic_course);
                bottom_bar_text_1.setTextColor(Color.parseColor("#000000"));

                bottom_bar_image_3.setImageResource(R.mipmap.ic_message);
                bottom_bar_text_3.setTextColor(Color.parseColor("#000000"));

                bottom_bar_image_4.setImageResource(R.mipmap.ic_my);
                bottom_bar_text_4.setTextColor(Color.parseColor("#000000"));

                break;

            case 2:
                //图片点击选择变换图片,颜色的改变,其他变为原来的颜色,并保持原来的图片
                bottom_bar_image_3.setImageResource(R.mipmap.ic_message_selected);
                bottom_bar_text_3.setTextColor(Color.parseColor("#0097F7"));

                //其他文本和颜色不变
                bottom_bar_image_2.setImageResource(R.mipmap.ic_exercise);
                bottom_bar_text_2.setTextColor(Color.parseColor("#000000"));

                bottom_bar_image_1.setImageResource(R.mipmap.ic_course);
                bottom_bar_text_1.setTextColor(Color.parseColor("#000000"));

                bottom_bar_image_4.setImageResource(R.mipmap.ic_my);
                bottom_bar_text_4.setTextColor(Color.parseColor("#000000"));

                break;

            case 3:
                //图片点击选择变换图片,颜色的改变,其他变为原来的颜色,并保持原来的图片
                bottom_bar_image_4.setImageResource(R.mipmap.ic_my_selected);
                bottom_bar_text_4.setTextColor(Color.parseColor("#0097F7"));

                //其他文本和颜色不变
                bottom_bar_image_2.setImageResource(R.mipmap.ic_exercise);
                bottom_bar_text_2.setTextColor(Color.parseColor("#000000"));

                bottom_bar_image_3.setImageResource(R.mipmap.ic_message);
                bottom_bar_text_3.setTextColor(Color.parseColor("#000000"));

                bottom_bar_image_1.setImageResource(R.mipmap.ic_course);
                bottom_bar_text_1.setTextColor(Color.parseColor("#000000"));
                break;
        }
    }

我们到testFragment里onCreate方法中获得Activity中实现好的实例化接口对象

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
        }
        titleCallback=(MainActivity)getActivity();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view= inflater.inflate(R.layout.fragment_test, container, false);
        TextView tvContent=view.findViewById(R.id.tv_content);
        tvContent.setText(mParam1);
        if (titleCallback!=null){
            titleCallback.setTitle(mParam1);
        }
        return  view;
    }
    //定义回调接口
    private TitleCallback titleCallback;
    public  interface  TitleCallback{
        void setTitle(String title);
    }

到此我们就完成了一个简单的小功能啦(~ ̄▽ ̄)~

步骤:
1.在Fragment类中定义一个回调接口
2.在Activity中实现这个回调接口
3.在Fragment的onCreate方法中获得Activity中实现好的实例化接口对象
4.利用接口的对象进行传值

软件
前端设计
程序设计
Java相关