08-27 GridView(点中后,图片被蒙住)、Toast(点击后显示提示的内容,一会儿自动消失)、Dialog(点击后显示对话框)

GridView(点中后,图片被蒙住<蒙板>)

//**layout_gridview.xml**
<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>

//**mygridview.xml**   view的模板
<?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>

//**Fruit类**
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;
    }

}

//**MyGridAdapter类**
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;
    }
}

//Activity文件
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) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
![这里写图片描述](http://img.blog.csdn.net/20150827200759323)

Toast(点击后显示提示的内容,一会儿自动消失)

//**layout_toast.xml**
<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>

//**mytoast.xml**  自定义的Toast
<?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>

//Activity文件
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()){
            //一般的Toast
            case R.id.button_toast1:
                Toast toast1=Toast.makeText(getApplicationContext(),"我是一个Toast",Toast.LENGTH_SHORT);//定义Toast
                toast1.setGravity(Gravity.LEFT|Gravity.BOTTOM,0,0);//定义位置
                toast1.show();
                break;
            //富文本显示的Toast
            case R.id.button_toast2:
                Toast toast2=Toast.makeText(getApplicationContext(),"",Toast.LENGTH_SHORT);//定义Toast
                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;
            //自定义显示Toast
            case R.id.button_toast3:
                Toast toast3=new Toast(getApplicationContext());//定义Toast
                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) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
![一般的Toast](http://img.blog.csdn.net/20150827202051874)
![富文本显示的Toast](http://img.blog.csdn.net/20150827202114726)
![自定义的Toast](http://img.blog.csdn.net/20150827202150146)

Dialog(点击后显示对话框)

//**layout_dialog.xml**
<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>

//**Activity文件**
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;
        }

    }


    //弹出一般的Dialog
    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();
    }

    //弹出带选择的Dialog
    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();
    }

    //弹出带单选框的Dialog
    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];//将选择后结果传入sex
                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();
    }

    //弹出带多选框的Dialog
    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;//把选择后的结果传入isHobbyChecked
            }
        });
        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();//初始化hobby,使得每次启动时,都是默认的效果
                for (int i = 0; i <isHobbyChecked.length ; i++) {
                    if(isHobbyChecked[i]){//判断选项是否被选中
                        hobby.append(mHobby[i]+"、");//将选中的加入hobby中
                    }
                    mButton4.setText("你的爱好是:"+hobby);//将选择后的结果显示在按钮上
                }
            }
        });
        AlertDialog dialog=builder.create();
        dialog.show();
    }




    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
![最初的界面](http://img.blog.csdn.net/20150827210132726)
![一般的Dialog](http://img.blog.csdn.net/20150827210354469)
![一般的Dialog点击后产生的效果](http://img.blog.csdn.net/20150827210452546)
![带选择的Dialog](http://img.blog.csdn.net/20150827210609989)
![带选择的Dialog点击后产生的效果](http://img.blog.csdn.net/20150827210655669)
![带单选框的Dialog](http://img.blog.csdn.net/20150827210746196)
![带单选框的Dialog点击后产生的效果](http://img.blog.csdn.net/20150827210903731)
![带单选框的Dialog点击后产生的效果](http://img.blog.csdn.net/20150827211034555)
![带多选框的Dialog](http://img.blog.csdn.net/20150827211118160)
![带多选框的Dialog点击后产生的效果](http://img.blog.csdn.net/20150827211214985)
![带多选框的Dialog点击后产生的效果](http://img.blog.csdn.net/20150827211305519)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章