第七周Android(CheckBox /ImageView/ListView/ImageButton/AdapterView)

来源:互联网 发布:pvc地板知乎 编辑:程序博客网 时间:2024/06/06 05:34

CheckBox

<?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:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="爱好"        android:id="@+id/textView_sex"         />    <CheckBox            android:id="@+id/checbox_swim"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="游泳"/>    <CheckBox        android:id="@+id/checbox_sing"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="唱歌"/>    <CheckBox        android:id="@+id/checbox_dance"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="跳舞"/>    <CheckBox        android:id="@+id/checbox_readbook"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="看书"/>    <EditText        android:id="@+id/editText_password"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="请输入密码"/>    <CheckBox        android:id="@+id/checbox_isshowpassword"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="显示密码"        android:layout_gravity="right"/>    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="注册"/></LinearLayout>

效果:
checkBox

ImageView

<?xml version="1.0" encoding="utf-8"?><ScrollView xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="vertical">        <ImageView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:scaleType="centerInside"            android:src="@mipmap/tu"/>        <ImageView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:scaleType="fitCenter"            android:src="@mipmap/tu"/>             <!--fitCenter:按比例拉伸图片,拉伸后图片的高度为View的高度,且显示在View的中间 -->        <ImageView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:scaleType="centerCrop"            android:src="@mipmap/tu"/>            <!--centerCrop:按比例放大原图直至等于某边View的宽高显示。-->        <ImageView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:scaleType="fitEnd"            android:src="@mipmap/tu"/>            <!--fitEnd:按比例拉伸图片,拉伸后图片的高度为View的高度,且显示在View的中间-->        <ImageView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:scaleType="fitStart"            android:src="@mipmap/tu"/>            <!--fitStart:按比例拉伸图片,拉伸后图片的高度为View的高度,且显示在View的左边-->        <ImageView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:scaleType="matrix"            android:src="@mipmap/tu"/>            <!--matrix:用矩阵来绘图-->        <ImageView            android:id="@+id/imageView"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:background="@mipmap/tu"/>        <ImageView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:scaleType="fitXY"            android:tint="#30DA70D6"            android:src="@mipmap/tu"/>            <!--tint:为图片设置着色颜色。-->            <!--fitXY:拉伸图片(不按比例)以填充View的宽高-->        <ImageView            android:id="@+id/imageView_two"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:scaleType="fitXY"            android:src="@mipmap/tuzi"/>        <RelativeLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="horizontal">            <Button                android:id="@+id/button_add"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="增加透明度"/>            <Button                android:id="@+id/button_sub"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="减少透明度"                android:layout_alignParentTop="true"                android:layout_alignParentRight="true"                android:layout_alignParentEnd="true" />        </RelativeLayout>    </LinearLayout></ScrollView>***********************************************************public class ImageActivity  extends Activity implements View.OnCreateContextMenuListener, View.OnClickListener {    private int mAlphaCount;    private Button mButton_add;    private Button mButton_sub;    private ImageView mImageView_two;    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_imagc);        mButton_add= (Button) findViewById(R.id.button_add);        mButton_sub= (Button) findViewById(R.id.button_sub);        mImageView_two= (ImageView) findViewById(R.id.imageView_two);        mImageView_two.setImageResource(R.mipmap.tuzi);        mButton_add.setOnClickListener(this);        mButton_sub.setOnClickListener(this);    }    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)    @Override    public void onClick(View v) {      int code=android.os.Build.VERSION.SDK_INT;        switch (v.getId()){            case R.id.button_add:                mAlphaCount+=5;                Log.d("versioncode","当前手机的版本号是:"+code);                if (code<16){                    mImageView_two.setAlpha(mAlphaCount);                }else{                    mImageView_two.setImageAlpha(mAlphaCount);                }                break;            case R.id.button_sub:                mAlphaCount-=5;                mImageView_two.setImageAlpha(mAlphaCount);                break;            default:                break;        }    }}

效果:
效果

ListView

<?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">    <ListView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:id="@+id/listview">    </ListView></LinearLayout>**************************************<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_margin="15dp"    android:layout_width="match_parent"    android:layout_height="match_parent">    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:orientation="horizontal">        <ImageView            android:id="@+id/imageView_phono"            android:layout_width="wrap_content"            android:layout_height="wrap_content"/>        <TextView            android:id="@+id/text_name"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="姓名">        </TextView>        <LinearLayout            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:orientation="vertical">            <TextView                android:id="@+id/text_age"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="年龄">            </TextView>            <TextView                android:id="@+id/text_sex"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="性别">            </TextView>        </LinearLayout>        <TextView            android:id="@+id/text_hobby"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="爱好">        </TextView>    </LinearLayout></LinearLayout>*********************************************public class ListTestActivity extends Activity{    private ListView mListView;    private String[] array={"张三","李四","王五","赵六"};    private List<HashMap<String,String>> mData;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_list);        mListView= (ListView) findViewById(R.id.listview);        initData();        SimpleAdapter adapter=new SimpleAdapter(this,mData,R.layout.activity_hobby,new String[]{"name","age","sex","hobby"},                new int[]{R.id.text_name,R.id.text_age,R.id.text_sex,R.id.text_hobby});        mListView.setAdapter(adapter);           private void initData(){        mData = new ArrayList<>();        HashMap<String,String> zhangsan = creatHashManp("张三","20","男","爱好打篮球");        mData.add(zhangsan);        HashMap<String,String> lisi = creatHashManp("李四","21","男","爱好打游戏");        mData.add(lisi);        HashMap<String,String> wangwu = creatHashManp("王五","19","女","爱好看书");        mData.add(wangwu);        HashMap<String,String> zhaoliu = creatHashManp("赵六","18","女","爱好唱歌");        mData.add(zhaoliu);    }    private HashMap<String,String>creatHashManp(String name,String age,String sex,String hobby){        HashMap<String,String> zhangsan=new HashMap<>();        zhangsan.put("name",name);        zhangsan.put("age",age);        zhangsan.put("sex",sex);        zhangsan.put("hobby",hobby);        return zhangsan;    }}

ImageButton

<ImageButton            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:src="@mipmap/tuzi"/>

AdapterView

 //AdapterView不允许使用OnClickListener        //mListView.setOnItemLongClickListenner();长按        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {            @Override            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {                //position是指的点击中的第position个数据                HashMap<String ,String> itemData=mData.get(position);                Log.d("data",""+itemData.get("name")+itemData.get("age")+itemData.get("sex")+itemData.get("hobby"));            }        });    }

自定义Adapater

public class Student {    private String age;    private String name;    private String sex;    private String hobby;    private int img;    public Student(String name,String age,String sex,String hobby,int img){        this.name=name;        this.age=age;        this.sex=sex;        this.hobby=hobby;        this.img=img;    }    public int getImg() {        return img;    }    public void setImg(int img) {        this.img = img;    }    public String getAge() {        return age;    }    public void setAge(String age) {        this.age = age;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getSex() {        return sex;    }    public void setSex(String sex) {        this.sex = sex;    }    public String getHobby() {        return hobby;    }    public void setHobby(String hobby) {        this.hobby = hobby;    }}************************************public class StudentAdapter extends BaseAdapter {    private List<Student> mData;    private LayoutInflater mInflater;    public StudentAdapter(LayoutInflater inflater, List<Student> data){        //将inflater        mInflater=inflater;        mData=data;    }    @Override    public int getCount() {        //得到listaview将要显示的数据条数        return mData.size();    }    @Override    public Object getItem(int position) {        //返回索引        return position;    }    @Override    public long getItemId(int position) {        //返回索引        return position;    }    @Override    public View getView(int position, View convertView, ViewGroup parent) {        View view=mInflater.inflate(R.layout.activity_hobby,null);        Student student=mData.get(position);        ImageView imageView= (ImageView) view.findViewById(R.id.imageView_phono);        TextView textView_name= (TextView) view.findViewById(R.id.text_name);        TextView textView_age= (TextView) view.findViewById(R.id.text_age);        TextView textView_sex= (TextView) view.findViewById(R.id.text_sex);        TextView textView_hobby= (TextView) view.findViewById(R.id.text_hobby);        textView_name.setText(student.getName());        textView_age.setText(student.getAge());        textView_sex.setText(student.getSex());        textView_hobby.setText(student.getHobby());        imageView.setImageResource(student.getImg());        return view;    }}***************************************public class ListTestActivity extends Activity{    private ListView mListView;    private List<Student> mData;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_list);        mListView= (ListView) findViewById(R.id.listview);        LayoutInflater inflater=getLayoutInflater();        initData();        StudentAdapter adapter=new StudentAdapter(inflater,mData);        mListView.setAdapter(adapter);    }    private void initData(){        mData = new ArrayList<>();        Student zhangsan = new Student("张三","20","男","爱好打篮球",R.mipmap.tu);        mData.add(zhangsan);        Student lisi = new Student("李四","21","男","爱好打游戏",R.mipmap.tu);        mData.add(lisi);        Student wangwu = new Student("王五","19","女","爱好看书",R.mipmap.tu);        mData.add(wangwu);        Student zhaoliu = new Student("赵六","18","女","爱好唱歌",R.mipmap.tu);        mData.add(zhaoliu);    }}效果:![这里写图片描述](http://img.blog.csdn.net/20150824193700438)
0 0