常用控件

ProgressBar控件的xml
<?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">


    <ProgressBar
        android:id="@+id/pb_bar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv_pb"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="下载"
        android:onClick="download"/>
</LinearLayout>

ProgressBar的Java代码

package com.example.app10;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private ProgressBar pb_progressBar;
    private TextView tv_textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        pb_progressBar = (ProgressBar)findViewById(R.id.pb_progressBar);
        tv_textView = (TextView) findViewById(R.id.tv_textView);
    }
    public void download(View view){
        new MyThread().start();
    }
    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            int i=msg.what;
            tv_textView.setText(i+"%");
        }
    };

    class MyThread extends Thread{
        @Override
        public void run() {
            super.run();
            for (int i = 0; i <=100 ; i++) {
                pb_progressBar.setProgress(i);
                handler.sendEmptyMessage(i);
                if(i==50) {
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }else{
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.app9.MainActivity">

    <ImageView
        android:layout_width="400dp"
        android:layout_height="400dp"
        android:id="@+id/img_main_img1"
        android:background="#6600ff00"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <RadioGroup
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:id="@+id/radio_rbs"
            >
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="1号佳丽"
                android:ems="3"
                />
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2号佳丽"
                android:ems="3"
                />
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="3号佳丽"
                android:ems="3"
                />
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="4号佳丽"
                android:ems="3"
                />
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="5号佳丽"
                android:ems="3"
                />
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="6号佳丽"
                android:ems="3"
                />
        </RadioGroup>

    </LinearLayout>

</LinearLayout>
第一种

package com.example.app9;

import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.RadioGroup;

public class MainActivity extends AppCompatActivity {

    private ImageView img;
    private RadioGroup rbs;
    private int images[]={
            R.drawable.s1,R.drawable.s2,R.drawable.s3,R.drawable.s4,R.drawable.s5,R.drawable.s6,R.drawable.s7
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        img = (ImageView) findViewById(R.id.img_main_img1);
        rbs = (RadioGroup) findViewById(R.id.radio_rbs);

        //给ImageView设置初始图片
        img.setImageResource(images[0]);

        rbs.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                //切换图片
                img.setImageResource(images[checkedId]);
            }
        });
    }
}
切换图片的效果图

第二种方法

用Environment类可以得到当前手机的一些信息,包括存储卡的信息

package com.example.app9;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.RadioGroup;

import java.io.File;

public class MainActivity extends AppCompatActivity {

    private ImageView img;
    private RadioGroup rbs;
    File[] files;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        img = (ImageView) findViewById(R.id.img_main_img1);
        rbs = (RadioGroup) findViewById(R.id.radio_rbs);
         //判断SD是否存在(如果你的手机支持SD扩展)
        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
         //得到存储卡的位置
        String s=Environment.getExternalStorageDirectory().getAbsolutePath();
            File file=new File(s+"/images");
            files=file.listFiles();
        }
        Bitmap bm= BitmapFactory.decodeFile(files[0].getAbsolutePath());
        img.setImageBitmap(bm);
        rbs.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                //切换图片
                Bitmap bm= BitmapFactory.decodeFile(files[checkedId].getAbsolutePath());
                img.setImageBitmap(bm);
            }
        });
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值