常用控件

xiaoxiao2021-02-28  61

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);             }         });     } }
转载请注明原文地址: https://www.6miu.com/read-97320.html

最新回复(0)