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)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章