在上一篇博客中,我们写了一个GridView来展示多列图片。现在我们修改一下代码,在每张图片添上一个CheckBox,当选中该图片时,添加上一层蒙版。
自定义布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" >
<ImageView android:id="@+id/imageview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true"/>
<TextView android:id="@+id/textview" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/imageview" android:layout_alignParentBottom="true" android:gravity="center"/>
<ImageView android:id="@+id/image_tint" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignRight="@id/textview" android:layout_alignBottom="@id/textview" android:background="#55ff0000" android:visibility="invisible"/>
<CheckBox android:id="@+id/checkbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:focusable="false" />
</RelativeLayout>
适配器MyGridAdapter
import java.util.ArrayList;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;
public class MyGridAdapter extends BaseAdapter{
private LayoutInflater inflater;
private ArrayList<Fruit> list;
private boolean[] manageCheckBox;
public MyGridAdapter(LayoutInflater inflater, ArrayList<Fruit> list) {
this.inflater = inflater;
this.list = list;
manageCheckBox=new boolean[list.size()];
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
Fruit fruit =list.get(position);
View view;
ViewHolder viewHolder;
if(convertView==null){
view=inflater.inflate(R.layout.fruit_gridview, null);
viewHolder=new ViewHolder();
viewHolder.image=(ImageView) view.findViewById(R.id.imageview);
viewHolder.name=(TextView) view.findViewById(R.id.textview);
viewHolder.image_tint=(ImageView) view.findViewById(R.id.image_tint);
viewHolder.checkbox=(CheckBox) view.findViewById(R.id.checkbox);
view.setTag(viewHolder);
}else{
view=convertView;
viewHolder=(ViewHolder) view.getTag();
}
viewHolder.image.setImageResource(fruit.getImage());
viewHolder.name.setText(fruit.getName());
viewHolder.checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
manageCheckBox[position]=isChecked;
notifyDataSetChanged();//刷新界面
}
});
viewHolder.checkbox.setChecked(manageCheckBox[position]);
if(manageCheckBox[position]){
//如果选中,显示蒙版
viewHolder.image_tint.setVisibility(View.VISIBLE);
}else{
viewHolder.image_tint.setVisibility(View.INVISIBLE);
}
return view;
}
class ViewHolder{
ImageView image;
TextView name;
ImageView image_tint;
CheckBox checkbox;
}
}
Toast是Android系统提供的一种非常好的提醒方式,在程序中可以用它将一些短小的信息通知给用户,这些信息会在一段时间后自动消失,并且不会占用任何屏幕空间。
通过调用静态方法makeText()创建一个Toast对象,然后调用show()方法将Toast显示出来。
Toast.makeText(getApplicationContext(),"",Toast.LENGTH_SHORT).show();
其中makeText()方法传入的三个参数,第一个参数是Context,也就是Toast要求的上下文,可以传入getApplicationContext(),也可以传入当前活动本身。第二个参数传入你想要显示的文本内容。第三个参数是Toast的显示时长,有两个内置常量可以选择Toast.LENGTH_SHORT和Toast.LENGTH_LONG。
setText() 设置展示的文本信息,可以传入一个富文本
setDuration() 设置展示的时间
setGravity(int gravity, int xOffset,int yOffset) 设置信息显示的位置
show() 显示Toast
Toast toast = new Toast(getApplicationContext());
自定义布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >
<TextView android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" />
<ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>
MainActivity
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button_toast=(Button) findViewById(R.id.button_toast);
button_toast.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast toast = new Toast(getApplicationContext());
LayoutInflater inflater=getLayoutInflater();
View view=inflater.inflate(R.layout.toast, null);
TextView textview=(TextView) view.findViewById(R.id.textview);
textview.setText("我是一个自定义的Toast");
ImageView image =(ImageView) view.findViewById(R.id.image);
image.setImageResource(R.drawable.weheartit);
toast.setView(view);
toast.setDuration(Toast.LENGTH_SHORT);
toast.show();
}
});
}
}
AlertDialog可以在当前界面弹出一个对话框,这个对话框是置顶于所有界面元素之上的,能够屏蔽其他控件的交互能力,因此一般AlertDialog都是用于提示一些非常重要的内容或警告信息。
AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnMultiChoiceClickListener;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button_dialog=(Button) findViewById(R.id.button_dialog);
button_dialog.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
dialog.setIcon(R.drawable.crert_delete);
dialog.setTitle("警告!");
dialog.setMessage("你确定要删除这个文件吗?");
dialog.setCancelable(false);
//为确定按钮设置点击事件
dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "您已成功删除该文件!", Toast.LENGTH_SHORT).show();
}
});
//为中性按钮设置点击事件
dialog.setNeutralButton("我不知道", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "您删不删,自己不知道啊?", Toast.LENGTH_SHORT).show();
}
});
//为取消按钮设置点击事件
dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "您已取消删除该文件!", Toast.LENGTH_SHORT).show();
}
});
dialog.show();
}
});
}
}
调用setItems()方法可以将自定义的内容作为选择内容,弹出对话框供用户选择。
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnMultiChoiceClickListener;
public class MainActivity extends Activity {
private String[] data={"第一个选择","第二个选择","第三个选择"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button_mydialog = (Button) findViewById(R.id.button_mydialog);
button_mydialog.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
dialog.setTitle("有选择内容的Dialog");
dialog.setItems(data, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//which索引从0开始
Toast.makeText(getApplicationContext(), "您选择了"+data[which], Toast.LENGTH_SHORT).show();
}
});
dialog.show();
}
});
}
}
运行结果:点击按钮,弹出对话框
点击第一个选择,显示选择信息
调用setSingleChoiceItems()方法,可以弹出单选的对话框。
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnMultiChoiceClickListener;
public class MainActivity extends Activity {
private String[] sex={"男","女","其他"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button_single = (Button) findViewById(R.id.button_singledialog);
button_single.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder dialog= new AlertDialog.Builder(MainActivity.this);
//第二个参数为默认选择的项,0 为第一项
dialog.setSingleChoiceItems(sex, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "您选择的性别为"+sex[which], Toast.LENGTH_SHORT).show();
}
});
dialog.show();
}
});
}
}
调用setMultiChoiceItems()方法,可以弹出多选的对话框。
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnMultiChoiceClickListener;
public class MainActivity extends Activity {
private String[] hobby={"篮球","足球","乒乓球","游泳","跑步","健身"};
private boolean[] isChooseHobby=new boolean[hobby.length];
private StringBuffer checkedHobby;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
button_multi.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder dialog=new AlertDialog.Builder(MainActivity.this);
dialog.setMultiChoiceItems(hobby, isChooseHobby, new OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
//将选中的该项设置为isChecked
isChooseHobby[which]=isChecked;
}
});
dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
checkedHobby=new StringBuffer();
//遍历全部选项,如果为true,将内容加入checkedHobby
for(int i=0;i<hobby.length;i++){
if(isChooseHobby[i]){
checkedHobby.append(hobby[i]);
}
}
Toast.makeText(getApplicationContext(), "您选择的兴趣为"+checkedHobby, Toast.LENGTH_SHORT).show();
}
});
dialog.show();
}
});
}
}