GridView(點中後,圖片被矇住<蒙板>)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<GridView
android:id="@+id/gridview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="3"></GridView>
</RelativeLayout>
<?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"
android:gravity="center"
android:orientation="vertical"
android:padding="3dp">
<ImageView
android:id="@+id/imageview_grid"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:layout_margin="15dp"
android:src="@mipmap/apple" />
<TextView
android:id="@+id/textview_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/imageview_grid"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="蘋果" />
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:layout_alignParentRight="true"/>
<!-- 添加蒙板-->
<ImageView
android:id="@+id/imageview_tint"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#55ff0000"
android:layout_alignRight="@id/textview_name"
android:layout_alignBottom="@id/textview_name"
android:visibility="invisible" />
</RelativeLayout>
public class Fruit {
private int imageView;
private String textView;
private boolean isChecked;
public Fruit(int imageView, String textView) {
this.imageView = imageView;
this.textView = textView;
}
public int getImageView() {
return imageView;
}
public void setImageView(int imageView) {
this.imageView = imageView;
}
public String getTextView() {
return textView;
}
public void setTextView(String textView) {
this.textView = textView;
}
public boolean isChecked() {
return isChecked;
}
public void setIsChecked(boolean isChecked) {
this.isChecked = isChecked;
}
}
public class MyGridAdapter extends BaseAdapter{
private List<Fruit> mFruits;
private LayoutInflater mInflater;
private boolean[] mMangerCheckBox;
public MyGridAdapter(List<Fruit> mFruits, LayoutInflater mInflater) {
this.mFruits = mFruits;
this.mInflater = mInflater;
mMangerCheckBox=new boolean[mFruits.size()];
}
@Override
public int getCount() {
return mFruits.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) {
ViewHolder viewHolder=null;
if(convertView==null){
viewHolder=new ViewHolder();
convertView=mInflater.inflate(R.layout.mygridview,null);
viewHolder.imageView= (ImageView) convertView.findViewById(R.id.imageview_grid);
viewHolder.textView= (TextView) convertView.findViewById(R.id.textview_name);
viewHolder.imageViewTint= (ImageView) convertView.findViewById(R.id.imageview_tint);
viewHolder.checkBox= (CheckBox) convertView.findViewById(R.id.checkbox);
convertView.setTag(viewHolder);
}else{
viewHolder= (ViewHolder) convertView.getTag();
}
Fruit fruit=mFruits.get(position);
viewHolder.imageView.setImageResource(fruit.getImageView());
viewHolder.textView.setText(fruit.getTextView());
viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mMangerCheckBox[position]=isChecked;
notifyDataSetChanged();
}
});
viewHolder.checkBox.setChecked(mMangerCheckBox[position]);
if(mMangerCheckBox[position]==true){
viewHolder.imageViewTint.setVisibility(View.VISIBLE);
}else {
viewHolder.imageViewTint.setVisibility(View.INVISIBLE);
}
return convertView;
}
class ViewHolder{
ImageView imageView;
TextView textView;
ImageView imageViewTint;
CheckBox checkBox;
}
}
public class MainActivity extends Activity {
private GridView mGridView;
private List<Fruit> mFruits;
private LayoutInflater mInflater;
private MyGridAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGridView= (GridView) findViewById(R.id.gridview);
mInflater=getLayoutInflater();
mFruits=new ArrayList<>();
for (int i = 0; i <3 ; i++) {
Fruit apple = new Fruit(R.mipmap.apple,"蘋果");
Fruit banana = new Fruit(R.mipmap.banana,"香蕉");
Fruit cherry = new Fruit(R.mipmap.cherry,"櫻桃");
Fruit peach = new Fruit(R.mipmap.peach,"桃子");
mFruits.add(apple);
mFruits.add(banana);
mFruits.add(cherry);
mFruits.add(peach);
}
mAdapter=new MyGridAdapter(mFruits,mInflater);
mGridView.setAdapter(mAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
![這裏寫圖片描述](http:
Toast(點擊後顯示提示的內容,一會兒自動消失)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<Button
android:id="@+id/button_toast1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="彈出Toast"/>
<Button
android:id="@+id/button_toast2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我也彈出Toast"
android:layout_below="@+id/button_toast1"/>
<Button
android:id="@+id/button_toast3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自定義Toast"
android:layout_below="@+id/button_toast2"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textview_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是title"/>
<ImageView
android:id="@+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>
<TextView
android:id="@+id/textview_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是內容"/>
</LinearLayout>
public class MainActivity extends Activity implements View.OnClickListener{
private Button mBtnToast1;
private Button mBtnToast2;
private Button mBtnToast3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBtnToast1= (Button) findViewById(R.id.button_toast1);
mBtnToast2= (Button) findViewById(R.id.button_toast2);
mBtnToast3= (Button) findViewById(R.id.button_toast3);
mBtnToast1.setOnClickListener(this);
mBtnToast2.setOnClickListener(this);
mBtnToast3.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button_toast1:
Toast toast1=Toast.makeText(getApplicationContext(),"我是一個Toast",Toast.LENGTH_SHORT);
toast1.setGravity(Gravity.LEFT|Gravity.BOTTOM,0,0);
toast1.show();
break;
case R.id.button_toast2:
Toast toast2=Toast.makeText(getApplicationContext(),"",Toast.LENGTH_SHORT);
Spanned spanned= Html.fromHtml("我也是<img src=''/>一個<font color='#55ff0000'>Toast</font>", new Html.ImageGetter() {
@Override
public Drawable getDrawable(String source) {
Drawable drawable=getResources().getDrawable(R.mipmap.ic_launcher);
drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());
return drawable;
}
},null);
toast2.setText(spanned);
toast2.setDuration(Toast.LENGTH_SHORT);
toast2.show();
break;
case R.id.button_toast3:
Toast toast3=new Toast(getApplicationContext());
LayoutInflater layoutInflater=getLayoutInflater();
View toastview=layoutInflater.inflate(R.layout.mytoast,null);
TextView textView_title= (TextView) toastview.findViewById(R.id.textview_title);
textView_title.setText("我是Toast的標題");
TextView textView_content= (TextView) toastview.findViewById(R.id.textview_content);
textView_content.setText("我是toas的內容");
toast3.setView(toastview);
toast3.setDuration(Toast.LENGTH_SHORT);
toast3.show();
break;
default:
break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
![一般的Toast](http:
![富文本顯示的Toast](http:
![自定義的Toast](http:
Dialog(點擊後顯示對話框)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="彈出最簡單的Dialog"/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="彈出帶選擇的Dialog"
android:layout_below="@+id/button1"/>
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="彈出帶單選框的Dialog"
android:layout_below="@+id/button2" />
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="彈出帶多選框的Dialog"
android:layout_below="@+id/button3" />
</RelativeLayout>
public class MainActivity extends Activity implements View.OnClickListener{
private Button mButton1;
private Button mButton2;
private Button mButton3;
private Button mButton4;
private String[] mDate={"第一條數據","第二條數據","第三條數據"};
private String[] mSexs={"男","女"};
private String sex=mSexs[0];;
private String[] mHobby={"足球","籃球","網球","游泳","跑步","旅遊"};
private boolean[] isHobbyChecked=new boolean[6];
private StringBuffer hobby;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton1= (Button) findViewById(R.id.button1);
mButton2= (Button) findViewById(R.id.button2);
mButton3= (Button) findViewById(R.id.button3);
mButton4= (Button) findViewById(R.id.button4);
mButton1.setOnClickListener(this);
mButton2.setOnClickListener(this);
mButton3.setOnClickListener(this);
mButton4.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button1:
showdialogsimple();
break;
case R.id.button2:
showdialogchoice();
break;
case R.id.button3:
showdialogsingle();
break;
case R.id.button4:
showdialogmulit();
break;
}
}
private void showdialogsimple() {
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.mipmap.ic_launcher);
builder.setTitle("我是Dialog的標題");
builder.setMessage("我是Dialog的內容");
builder.setNegativeButton("Negative", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "點擊的是NegativeButton", Toast.LENGTH_SHORT).show();
}
});
builder.setNeutralButton("Neutral", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(),"點擊的是NeutralButton",Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("Positive", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(),"點擊的是PositiveButton",Toast.LENGTH_SHORT).show();
}
});
AlertDialog dialog=builder.create();
dialog.show();
}
private void showdialogchoice() {
AlertDialog.Builder builder1=new AlertDialog.Builder(MainActivity.this);
builder1.setIcon(R.mipmap.ic_launcher);
builder1.setTitle("帶選擇的Dialog");
builder1.setItems(mDate, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "選中的是第" + (which + 1) + "條數據", Toast.LENGTH_SHORT).show();
}
});
AlertDialog dialog1=builder1.create();
dialog1.show();
}
private void showdialogsingle() {
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.mipmap.ic_launcher);
builder.setTitle("帶單選框的Dialog");
builder.setSingleChoiceItems(mSexs,0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
sex=mSexs[which];
Toast.makeText(getApplicationContext(), "你選中的性別是:" + mSexs[which], Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mButton3.setText(sex);
}
});
AlertDialog dialog=builder.create();
dialog.show();
}
private void showdialogmulit() {
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.mipmap.ic_launcher);
builder.setTitle("帶多選框的Dialog");
builder.setMultiChoiceItems(mHobby, isHobbyChecked, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
isHobbyChecked[which]=isChecked;
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
hobby=new StringBuffer();
for (int i = 0; i <isHobbyChecked.length ; i++) {
if(isHobbyChecked[i]){
hobby.append(mHobby[i]+"、");
}
mButton4.setText("你的愛好是:"+hobby);
}
}
});
AlertDialog dialog=builder.create();
dialog.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
![最初的界面](http:
![一般的Dialog](http:
![一般的Dialog點擊後產生的效果](http:
![帶選擇的Dialog](http:
![帶選擇的Dialog點擊後產生的效果](http:
![帶單選框的Dialog](http:
![帶單選框的Dialog點擊後產生的效果](http:
![帶單選框的Dialog點擊後產生的效果](http:
![帶多選框的Dialog](http:
![帶多選框的Dialog點擊後產生的效果](http:
![帶多選框的Dialog點擊後產生的效果](http: