布局中通常会用到@null。如RadioButton常用的技巧通过RadioGroup实现Tab,需要设置android:button="@null"。如果要在代码中动态创建控件,android中并不能找到相关的属性或方法。搜索均无解决办法,最后想到一个变通的方法:通过透明色获取drawable。
setButtonDrawable(getResources().getDrawable(android.R.color.transparent))
实际还是可以通过布局的方法来动态创建控件。先创建一个RadioButton的rb.xml
<?xml version="1.0" encoding="utf-8"?> <RadioButton xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:button="@null" > </RadioButton>
再在代码生成RadioButton
RadioButton rb =(RadioButton)LayoutInflater.from(getContext()).inflate(R.layout.rb, null);
这种方式的好处是样式等属性可以在布局中统一指定省的查sdk寻找相关属性的设置方法。
package com.duoguo.android;
import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
/**
* 1、CheckBox的使用; 2、RadioButton的使用; 3、Toast的使用。
*
* @author shyboy(897948924@qq.com)
*
*/
public class ButtonActivityActivity extends Activity {
private RadioGroup genderRadioGroup;
private RadioButton maleRadioButton;
private RadioButton femaleRadioButton;
private CheckBox footballCheckBox;
private CheckBox basketballCheckBox;
private CheckBox volleyballCheckBox;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
genderRadioGroup = (RadioGroup) findViewById(R.id.genderRadioGroupId);
maleRadioButton = (RadioButton) findViewById(R.id.maleRadioButtonId);
femaleRadioButton = (RadioButton) findViewById(R.id.femaleRadioButtonId);
footballCheckBox = (CheckBox) findViewById(R.id.footballCheckBoxId);
basketballCheckBox = (CheckBox) findViewById(R.id.basketballCheckBoxId);
volleyballCheckBox = (CheckBox) findViewById(R.id.volleyballCheckBoxId);
// 为RadioGroup添加单击监听事件
genderRadioGroup
.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (maleRadioButton.getId() == checkedId)// 当单选按钮被选中时
{
System.out.println("male");
Toast.makeText(ButtonActivityActivity.this,
"您选择的是" + maleRadioButton.getText(),
Toast.LENGTH_LONG).show();
} else if (femaleRadioButton.getId() == checkedId) {
System.out.println("female");
Toast.makeText(ButtonActivityActivity.this,
"您选择的是:" + femaleRadioButton.getText(),
Toast.LENGTH_LONG).show();
}
}
});
// 为CheckBox添加单击监听事件
footballCheckBox
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
System.out.println("football");
Toast.makeText(ButtonActivityActivity.this,
"原来您的爱好是:" + footballCheckBox.getText(),
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(ButtonActivityActivity.this,
"原来您不喜欢的是:" + footballCheckBox.getText(),
Toast.LENGTH_LONG).show();
}
}
});
basketballCheckBox
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
System.out.println("basketball");
Toast.makeText(ButtonActivityActivity.this,
"原来您的爱好是:" + basketballCheckBox.getText(),
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(ButtonActivityActivity.this,
"原来您不喜欢的是:" + basketballCheckBox.getText(),
Toast.LENGTH_LONG).show();
}
}
});
volleyballCheckBox
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
System.out.println("volleyball");
Toast.makeText(ButtonActivityActivity.this,
"原来您的爱好是:" + volleyballCheckBox.getText(),
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(ButtonActivityActivity.this,
"原来您不喜欢的是:" + volleyballCheckBox.getText(),
Toast.LENGTH_LONG).show();
}
}
});
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RadioGroup android:id="@+id/genderRadioGroupId"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton android:id="@+id/maleRadioButtonId"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="@string/male" />
<RadioButton android:id="@+id/femaleRadioButtonId"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="@string/female" />
</RadioGroup>
<CheckBox android:id="@+id/footballCheckBoxId"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="@string/football" />
<CheckBox android:id="@+id/basketballCheckBoxId"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="@string/basketball" />
<CheckBox android:id="@+id/volleyballCheckBoxId"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="@string/volleyball" />
</LinearLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">ButtonActivity</string>
<string name="male">男</string>
<string name="female">女</string>
<string name="football">足球</string>
<string name="basketball">篮球</string>
<string name="volleyball">排球</string>
</resources>
有三个view,分别为view1、view2、view3,通过UISegmentedControl进行三个view的切换。
@interface UIViewDemoViewController : UIViewController { IBOutlet UIView *view1; IBOutlet UIView *view2; IBOutlet UIView *view3; } - (IBAction)switchViews:(id)sender; @end
在Interface Builder中分别建立三个view,关联到各自的输出口,每个view上的UISegmentedControl关联到switchViews:操作。
- (void)viewDidLoad { [super viewDidLoad]; [self.view addSubview:view1]; [self.view addSubview:view2]; [self.view addSubview:view3]; } - (IBAction)switchViews:(id)sender{ UISegmentedControl *segmentedControl = sender; [[NSNotificationCenter defaultCenter] postNotificationName:@"switchViews" object:[NSNumber numberWithInteger:[segmentedControl selectedSegmentIndex]]]; }
接着,为UISegmentedControl控件建立一个处理类。
@interface SegmentedControl : UISegmentedControl { IBOutlet UIView *view1; IBOutlet UIView *view2; IBOutlet UIView *view3; IBOutlet UIViewDemoViewController* viewController; } @end
将UISegmentedControl控件的Class改为刚创建的处理类:SegmentedControl,然后,将UISegmentedControl控件关联到四个输出口:view分别关联到刚创建的三个view上,viewController关联到File's Owner上。
- (void)awakeFromNib{ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(switchViews:) name:@"switchViews" object:nil]; } - (void)switchViews:(NSNotification*)notification{ NSNumber *viewNumber = [notification object]; NSInteger i = [viewNumber integerValue]; [self setSelectedSegmentIndex:i]; UIView *chosenView = nil; switch (i) { case 0: chosenView = view1; break; case 1: chosenView = view2; break; case 2: chosenView = view3; break; default: break; } if (chosenView) { [[viewController view] bringSubviewToFront:chosenView]; } } - (void)dealloc{ [super dealloc]; [[NSNotificationCenter defaultCenter] removeObserver:self]; }