本案例实现了多选 按钮的全选,如果全部都选,全选按钮会自动选上,反之会取消。
单击下一个按钮,图片会循环显示图片!
1.main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/textoneid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#ff00ff"
android:textSize="14px"
android:text="北京****技有限公司"
/>
<Button
android:id="@+id/buttononeid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#34ff67"
android:textSize="14px"
android:text="按下我,给你一个惊喜"
/>
<TextView
android:id="@+id/texttwoid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#ff00ff"
android:textSize="14px"
android:text="你经常浏览的网站是:"
/>
<CheckBox
android:id="@+id/checkboxoneid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#45ff23"
android:text="www.baidu.com"
/>
<CheckBox
android:id="@+id/checkboxtwoid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#45ff23"
android:text="www.sina.com.cn"
/>
<CheckBox
android:id="@+id/checkboxthreeid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#45ff23"
android:text="www.eoeandroid.com"
/>
<CheckBox
android:id="@+id/checkboxfourid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#45ff23"
android:text="selectAll"
/>
<Button
android:id="@+id/buttontwoid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#34ff67"
android:textSize="14px"
android:text="下一个图片"
/>
<ImageView
android:id="@+id/p_w_picpathviewoneid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
/>
</LinearLayout>
2.MainActivity.java
package com.practise.androud_view;
import android.R.bool;
import android.R.integer;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.SearchView.OnCloseListener;
public class MainActivity extends Activity {
private CheckBox checkboxone;
private CheckBox checkboxtwo;
private CheckBox checkboxthree;
private CheckBox checkboxfour;
private Button privewButton;
private ImageView p_w_picpathView;
int count=1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
checkboxone=(CheckBox)findViewById(R.id.checkboxoneid);
checkboxtwo=(CheckBox)findViewById(R.id.checkboxtwoid);
checkboxthree=(CheckBox)findViewById(R.id.checkboxthreeid);
checkboxfour=(CheckBox)findViewById(R.id.checkboxfourid);
privewButton=(Button)findViewById(R.id.buttontwoid);
p_w_picpathView=(ImageView)findViewById(R.id.p_w_picpathviewoneid);
CheckBoxSelect cbs=new CheckBoxSelect();
checkboxone.setOnClickListener(cbs);
checkboxtwo.setOnClickListener(cbs);
checkboxthree.setOnClickListener(cbs);
CheckBoxSelectAll cbsaall=new CheckBoxSelectAll();
checkboxfour.setOnClickListener(cbsaall);
buttonSelectListener bsl=new buttonSelectListener();
privewButton.setOnClickListener(bsl);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
class CheckBoxSelectAll implements android.view.View.OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Boolean b=checkboxfour.isChecked();
if (b) {
checkboxone.setChecked(true);
checkboxtwo.setChecked(true);
checkboxthree.setChecked(true);
}
else {
checkboxone.setChecked(false);
checkboxtwo.setChecked(false);
checkboxthree.setChecked(false);
}
}
}
class CheckBoxSelect implements android.view.View.OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
CheckBox checkBox=(CheckBox)v;
boolean b=checkBox.isChecked();
String content=(String)checkBox.getText();
if (b) {
System.out.print("已被选中");
}else {
System.out.print("已取消选中");
}
Boolean bo=checkboxone.isChecked();
Boolean bt=checkboxtwo.isChecked();
Boolean bh=checkboxthree.isChecked();
if (bo&&bt&&bh) {
checkboxfour.setChecked(true);
}else{
checkboxfour.setChecked(false);
}
}
}
class buttonSelectListener implements android.view.View.OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Resources resources = getResources();
if(count%2==1){
count++;
Drawable drawable = resources.getDrawable(R.drawable.icon);
p_w_picpathView.setImageDrawable(drawable);
}else {
count--;
Drawable drawable = resources.getDrawable(R.drawable.ic_launcher);
p_w_picpathView.setImageDrawable(drawable);
}
}
}
}
转载于:https://blog.51cto.com/mxjy1208/934699