ITPub博客

首页 > 应用开发 > IT综合 > 在线直播源码如何实现一个分断Button

在线直播源码如何实现一个分断Button

IT综合 作者:云豹科技阿星 时间:2021-09-23 17:12:03 0 删除 编辑

功能:

    在线直播源码实现拼接的Button。

使用说明:

    在线直播源码用RidaoGroup包裹几个RidaoButton,实现拼接。

在线直播源码页面做的比较简单,这个功能也不太难。。这只是其中的实现方式之一。有其它更好的方式,请告之。

下面还是看代码吧:

界面Activity:

package com.bbswp.topbuttondemo;
import com.bbswp.topbuttondemo.view.SegmentedRadioGroup;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;
public class MainActivity extends Activity implements OnCheckedChangeListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SegmentedRadioGroup group = (SegmentedRadioGroup) findViewById(R.id.segment_text);
        group.setOnCheckedChangeListener(this);
    }
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.button_one:
                toast(1);
                break;
            case R.id.button_two:
                toast(2);
                break;
            case R.id.button_three:
                toast(3);
                break;
            default:
                break;
        }
    }
    public void onClick(View view) {
        toast(4);
    }
    private void toast(int id) {
        Toast.makeText(this, "点击:" + id, 0).show();
    }
}

自定义一个View,用于包裹RidaoButton.

看代码:

/*
 * Copyright (C) 2011 Make Ramen, LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bbswp.topbuttondemo.view;
import com.bbswp.topbuttondemo.R;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.RadioGroup;
public class SegmentedRadioGroup extends RadioGroup {
	public SegmentedRadioGroup(Context context) {
		super(context);
	}
	public SegmentedRadioGroup(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		changeButtonsImages();
	}
	private void changeButtonsImages(){
		int count = super.getChildCount();
		if(count > 1){
			super.getChildAt(0).setBackgroundResource(R.drawable.segment_radio_left);
			for(int i=1; i < count-1; i++){
				super.getChildAt(i).setBackgroundResource(R.drawable.segment_radio_mid);
			}
			super.getChildAt(count-1).setBackgroundResource(R.drawable.segment_radio_right);
		}else if (count == 1){
			super.getChildAt(0).setBackgroundResource(R.drawable.segment_radio_button);
		}
	}
}

界面少不了xml。。。看看吧:

<LinearLayout xmlns:android="
    xmlns:tools="
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >
    <com.bbswp.topbuttondemo.view.SegmentedRadioGroup
        android:id="@+id/segment_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dip"
        android:checkedButton="@+id/button_one"
        android:gravity="top|center"
        android:orientation="horizontal" >
        <RadioButton
            android:id="@id/button_one"
            android:button="@null"
            android:gravity="center"
            android:minHeight="33dip"
            android:minWidth="40dip"
            android:text="我是第一个"
            android:textAppearance="?android:attr/textAppearanceSmall" />
        <RadioButton
            android:id="@+id/button_two"
            android:button="@null"
            android:gravity="center"
            android:minHeight="33dip"
            android:minWidth="40dip"
            android:text="我是第二个"
            android:textAppearance="?android:attr/textAppearanceSmall" />
        <RadioButton
            android:id="@+id/button_three"
            android:button="@null"
            android:gravity="center"
            android:minHeight="33dip"
            android:minWidth="40dip"
            android:text="我是第三个"
            android:textAppearance="?android:attr/textAppearanceSmall" />
    </com.bbswp.topbuttondemo.view.SegmentedRadioGroup>
    <com.bbswp.topbuttondemo.view.SegmentedRadioGroup
        android:id="@+id/segment_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dip"
        android:checkedButton="@+id/button_one"
        android:gravity="center_horizontal"
        android:orientation="horizontal" >
        <RadioButton
            android:id="@+id/button_four"
            android:button="@null"
            android:gravity="center"
            android:minHeight="33dip"
            android:minWidth="40dip"
            android:onClick="onClick"
            android:text="我是单独的一个"
            android:textAppearance="?android:attr/textAppearanceSmall" />
    </com.bbswp.topbuttondemo.view.SegmentedRadioGroup>
</LinearLayout>

这里还有重要的四个drawable文件,用于按下的效果显示:

segment_radio_button.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android=">
<item android:state_enabled="false"  android:drawable="@drawable/segment_grey" />
    <item android:state_focused="true" android:state_checked="true" android:state_pressed="false" android:drawable="@drawable/segment_grey_focus" />
    <item android:state_focused="true" android:state_checked="false" android:state_pressed="false" android:drawable="@drawable/segment_white_focus" />
    
    <item android:state_pressed="true"  android:drawable="@drawable/segment_grey_press" /> 
    <item android:state_checked="false"  android:drawable="@drawable/segment_white" />
    <item android:drawable="@drawable/segment_grey" />  <!-- default  -->
</selector>

后面是左:

segment_radio_left.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android=">
 <item android:drawable="@drawable/segment_radio_grey_left_focus" android:state_checked="true" android:state_focused="true" android:state_pressed="false"/>
    <item android:drawable="@drawable/segment_radio_white_left_focus" android:state_checked="false" android:state_focused="true" android:state_pressed="false"/>
    <item android:drawable="@drawable/segment_radio_grey_left_press" android:state_pressed="true"/>
    <item android:drawable="@drawable/segment_radio_white_left" android:state_checked="false"/>
    <item android:drawable="@drawable/segment_radio_grey_left"/>
</selector>

中:

segment_radio_mid.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android=">
<item android:state_focused="true" android:state_checked="true" android:state_pressed="false" android:drawable="@drawable/segment_radio_grey_middle_focus" />
    <item android:state_focused="true" android:state_checked="false" android:state_pressed="false" android:drawable="@drawable/segment_radio_white_middle_focus" />
    
    <item android:state_pressed="true"  android:drawable="@drawable/segment_radio_grey_middle_press" /> 
    <item android:state_checked="false" android:drawable="@drawable/segment_radio_white_middle" /> 
    <item android:drawable="@drawable/segment_radio_grey_middle" /> 
</selector>

右:

segment_radio_right.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android=">
<item android:state_focused="true" android:state_checked="true" android:state_pressed="false" android:drawable="@drawable/segment_radio_grey_right_focus" />
    <item android:state_focused="true" android:state_checked="false" android:state_pressed="false" android:drawable="@drawable/segment_radio_white_right_focus" />
    
    <item android:state_pressed="true"  android:drawable="@drawable/segment_radio_grey_right_press" /> 
    <item android:state_checked="false" android:drawable="@drawable/segment_radio_white_right" />   
    <item android:drawable="@drawable/segment_radio_grey_right" />
</selector>

在线直播源码有更好的方式,一起交流吧!!!

声明:本文由云豹科技转发自hudan2714博客,如有侵权请联系作者删除


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/69982461/viewspace-2793348/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2020-08-24

  • 博文量
    229
  • 访问量
    78026