单选框
<RadioGroup
android:id="@+id/radio_g"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/radioMale"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="true"
android:text="男"
android:textSize="20sp" />
<RadioButton
android:id="@+id/radilFemale"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="女"
android:textSize="20sp" />
RadioGroup>
LinearLayout>
单选框的监听
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.radioMale:
sex = "男";
break;
case R.id.radilFemale:
sex = "女";
break;
default:
break;
}
}
});
复选框
"match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
id="@+id/box1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="玩游戏"
android:textSize="18sp" />
id="@+id/box2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="听音乐"
android:textSize="18sp" />
id="@+id/box3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="看电影"
android:textSize="18sp" />
id="@+id/box4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="敲代码"
android:textSize="18sp" />
复选框的监听
public void selectChange() {
box1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked)
box1Str = (String) buttonView.getText();
else {
box1Str = "";
}
}
});
box2......
}
对话框 选择性别(单选)
btn_sex.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder dialog = new AlertDialog.Builder(
FinshActivity.this);
dialog.setTitle("请选择");
dialog.setSingleChoiceItems(sexs, flag,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
flag = which;
}
});
dialog.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
btn_sex.setText(sexs[flag]);
}
});
dialog.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// TODO
}
});
dialog.show();
}
});
对话框 多选
// 个人爱好的监听事件
btn_hobby.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder dialog = new AlertDialog.Builder(
FinshActivity.this);
dialog.setTitle("请选择");
dialog.setMultiChoiceItems(hobbys, check,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which, boolean isChecked) {
check[which] = isChecked;
}
});
dialog.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
selectString ="";
for (int i = 0; i < hobbys.length; i++) {
if (check[i] == true) {
selectString = selectString + " "
+ hobbys[i];
btn_hobby.setText(selectString);
}
}
}
});
dialog.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// TODO
}
});
dialog.show();
}
});
对话框 修改密码(弹出一个新的视图) (附文件保存)
btn_changepwd.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder dialog = new AlertDialog.Builder(
FinshActivity.this);
View view = getLayoutInflater().inflate(R.layout.pwd_layout,
null);
newpwd = (EditText) view.findViewById(R.id.newpwd);
oknewpwd = (EditText) view.findViewById(R.id.oknewpwd);
dialog.setView(view);
dialog.setPositiveButton("确定", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String cpwd=newpwd.getText().toString();
String cokpwd=oknewpwd.getText().toString();
if (cpwd.equals(cokpwd)) {
try {
File file=new File(FinshActivity.this.getFilesDir(),"info.txt");
FileOutputStream fosStream =new FileOutputStream(file);
String info = qq+"##"+cpwd;
fosStream.write(info.getBytes());
fosStream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Toast.makeText(FinshActivity.this,"修改密码成功!", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(FinshActivity.this,"修改密码失败!", Toast.LENGTH_SHORT).show();
}
}
});
dialog.setNegativeButton("取消", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//TODO
}
});
dialog.show();
}
});
新建一个R.layout.pwd_layout
文件读取:try {
File file = new File(this.getFilesDir(), "info.txt");
FileInputStream fis = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String info = br.readLine();
qq = info.split("##")[0];
pas = info.split("##")[1];
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}