写一个页面,有广播、菜单、弹出框三个选项卡由RadioGroup+RadioButton和页面Fragment组成

主页面布局activity_main.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context="com.bwie.liuchongyang20180528.MainActivity">

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="5"
        android:orientation="vertical"/>

    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/btn_one"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="文字广播"
            android:background="@drawable/ic_launcher_foreground"
            android:layout_gravity="center"/>
        <RadioButton
            android:id="@+id/btn_two"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="上下文菜单"
            android:background="@drawable/ic_launcher_foreground"
            android:layout_gravity="center"/>
        <RadioButton
            android:id="@+id/btn_three"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="弹出对话框"
            android:background="@drawable/ic_launcher_foreground"
            android:layout_gravity="center"/>

    </RadioGroup>

</LinearLayout>

选项卡广播页面布局fragment_one.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="com.bwie.liuchongyang20180528.OneFragment">

    <Button
        android:id="@+id/send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="点击我,可以发送广播"/>

</LinearLayout>



选项卡菜单页面布局fragment_two.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="com.bwie.liuchongyang20180528.TwoFragment">

    <TextView
        android:id="@+id/menu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="长按本选框,可实现上下文菜单"/>

</LinearLayout>


选项卡弹出框页面布局fragment_three.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="com.bwie.liuchongyang20180528.ThreeFragment">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="点击我,可以弹出一个对话框"/>

</LinearLayout>

选项卡菜单页面上下文布局fragment_two_menu.xml文件

<?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">

    <Button
        android:id="@+id/btn01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="你好"/>
    <Button
        android:id="@+id/btn02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello"/>
    <Button
        android:id="@+id/btn03"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hi"/>

</LinearLayout>




values文件夹下styles.xml文件

<resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="android:Theme.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

    <style name="AnimationFade">

        <!-- PopupWindow左右弹出的效果 -->
        <item name="android:windowEnterAnimation">@anim/inputodown</item>
        <item name="android:windowExitAnimation">@anim/outdowntoup</item>
    </style>

</resources>


res文件夹下新建anim文件夹下inputodown.xml文件 和 outdowntoup.xml文件

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="500"
        android:fromYDelta="-100%"
        android:toYDelta="0" />

</set>


<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="500"
        android:fromYDelta="0"
        android:toYDelta="-100%" />

</set>



主布局MainActivity.java代码文件

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioGroup;

public class MainActivity extends AppCompatActivity {

    //声明控件属性名称
    private ViewGroup viewGroup;
    private Button btn_one;
    private Button btn_two;
    private Button btn_three;
    private RadioGroup radioGroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate ( savedInstanceState );
        setContentView ( R.layout.activity_main );
        initView();
        radioGroup.setOnCheckedChangeListener ( new RadioGroup.OnCheckedChangeListener () {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                Intent intent = new Intent (  );
                switch (checkedId){
                    case R.id.btn_one://点击跳转广播页面
                        intent.setClass ( MainActivity.this,OneFragment.class );
                        break;
                    case R.id.btn_two://点击跳转菜单页面
                        intent.setClass ( MainActivity.this,TwoFragment.class );
                        break;
                    case R.id.btn_three://点击跳转对话框页面
                        intent.setClass ( MainActivity.this,ThreeFragment.class );
                        break;
                }
                startActivity ( intent );
            }
        } );
    }

    /**
     * 初始化控件
     */
    private void initView() {
        viewGroup = (ViewGroup) findViewById ( R.id.viewPager );
        btn_one = (Button)findViewById ( R.id.btn_one );
        btn_two = (Button)findViewById ( R.id.btn_two );
        btn_three = (Button)findViewById ( R.id.btn_three );
        radioGroup = (RadioGroup)findViewById ( R.id.radioGroup );
    }

}


广播选项卡布局OneFragment.java代码文件

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class OneFragment extends Fragment implements View.OnClickListener {


    private Button sendBroadCast;

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


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
        View view = layoutInflater.inflate( R.layout.activity_one_fragment, null);

        initView(view);
        return view;
    }

    private void initView(View view) {
        sendBroadCast = (Button) view.findViewById(R.id.send);

        sendBroadCast.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.send:
                Intent intent = new Intent();
                intent.putExtra("OneFragment", "我是从OneFragment中的广播发送而来的");
                intent.setAction("OneFragment_action");//谁过滤这种action谁就可以拦截到这个广播
                getActivity().sendBroadcast(intent);

                break;
        }
    }
}


菜单选项卡布局TwoFragment.java代码文件

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;

public class TwoFragment extends Activity implements View.OnClickListener {

    private PopupWindow popupWindow;//菜单
    private TextView tv_menu;//文本框

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate ( savedInstanceState );
        setContentView ( R.layout.activity_two_fragment );
        tv_menu = (TextView)findViewById ( R.id.menu );
        tv_menu.setOnClickListener ( this );
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.menu:
                if (popupWindow != null && popupWindow.isShowing()) {
                    popupWindow.dismiss();
                    return;
                } else {
                    initmPopupWindowView();
                    popupWindow.showAsDropDown(v, 0, 5);
                }
                break;
            default:
                break;
        }

    }

    private void initmPopupWindowView() {
        // // 获取自定义布局文件pop.xml的视图
        View customView = getLayoutInflater().inflate(R.layout.two_fragment_menu,
                null, false);
        // 创建PopupWindow实例,200,150分别是宽度和高度
        popupWindow = new PopupWindow(customView, 250, 280);
        // 设置动画效果 [R.style.AnimationFade 是自己事先定义好的]
        popupWindow.setAnimationStyle(R.style.AnimationFade);
        // 自定义view添加触摸事件
        customView.setOnTouchListener(new View.OnTouchListener () {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (popupWindow != null && popupWindow.isShowing()) {
                    popupWindow.dismiss();
                    popupWindow = null;
                }

                return false;
            }
        });

        /** 在这里可以实现自定义视图的功能 */
        Button btton2 = (Button) customView.findViewById(R.id.btn01);
        Button btton3 = (Button) customView.findViewById(R.id.btn02);
        Button btton4 = (Button) customView.findViewById(R.id.btn03);
        btton2.setOnClickListener(this);
        btton3.setOnClickListener(this);
        btton4.setOnClickListener(this);

    }

}


弹出框选项卡布局ThreeFragment.java代码文件

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class ThreeFragment extends Activity {

    private Button btn;//对话框按钮

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

    /**
     * 初始化控件
     */
    private void initEvent() {
        findViewById ( R.id.button ).setOnClickListener ( new View.OnClickListener () {
            @Override
            public void onClick(View v) {
                showDialog();
            }
        } );
    }

    /**
     * 设置对话框
     */
    private void showDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder ( this );
        builder.setIcon ( R.mipmap.ic_launcher_round );//对话框中显示的机器人图片
        builder.setTitle ( "Dialog对话框" );//对话框中的标题
        builder.setMessage ( "是否要退出程序?" );//对话框中的内容
        builder.setNegativeButton ( "取消", new DialogInterface.OnClickListener () {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //确认退出按钮
            }
        } );
        builder.setPositiveButton ( "确认", new DialogInterface.OnClickListener () {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //取消退出按钮
            }
        } );
        AlertDialog dialog = builder.create ();
        dialog.show ();//显示对话框
    }
}

阅读更多
换一批

没有更多推荐了,返回首页