Android 常用组件

分类:Android     发布:2019-12-05     来源:本站     浏览:1273 次
运行效果:

前台代码 Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="AutoCompleteTextView:"
        android:textStyle="bold" />

    <AutoCompleteTextView
        android:id="@+id/txtAuto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:dropDownWidth="match_parent" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Switch:"
        android:textStyle="bold" />

    <Switch
        android:id="@+id/sw"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="13dp"
        android:text="开启横屏模式" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Switch:"
        android:textStyle="bold" />

    <RadioGroup
        android:id="@+id/radiogroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/radMan"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="男" />

        <RadioButton
            android:id="@+id/radWoMan"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女" />

    </RadioGroup>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ProgressBar:"
        android:textStyle="bold" />

    <ProgressBar
        android:id="@+id/progress"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:layout_width="match_parent"
        android:layout_height="25dp"
        android:max="100" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="SeekBar:使用拖动条设置屏幕亮度前,需要将手机自动调节屏幕亮度的功能关闭。"
        android:textStyle="bold" />

    <SeekBar
        android:id="@+id/seekBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RatingBar:星级评分"
        android:textStyle="bold" />
    <RatingBar
        android:id="@+id/ratbar"
        android:layout_width="wrap_content"
        android:numStars="6"
        android:rating="3"
        android:stepSize="1"
        android:layout_height="wrap_content"/>

</LinearLayout>
后台代码 java:
package com.qingshan.demo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.CompoundButton;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RatingBar;
import android.widget.SeekBar;
import android.widget.Switch;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    public static String[] book = new String[]{"Android教程", "Android入门到精通", "Android开发实战", "Android精彩编程", "Android范例宝典"};
    private ProgressBar progressBar;//水平进度条
    private int progress = 0;//完成进度
    private Handler handler;//声明一个用于处理消息的Handler类的对象

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

        //AutoCompleteTextView
        AutoCompleteTextView txt = findViewById(R.id.txtAuto);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.support_simple_spinner_dropdown_item, book);
        txt.setAdapter(adapter);

        //Switch
        final Switch sw = (Switch) findViewById(R.id.sw);
        sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (sw.isChecked()) {
                    Toast.makeText(MainActivity.this, "开启横屏!", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, "关闭横屏!", Toast.LENGTH_SHORT).show();
                }
            }
        });
        //RadioGroup
        final RadioGroup sex = (RadioGroup) findViewById(R.id.radiogroup);
        sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton rd = findViewById(checkedId);
                //for (int i = 0; i < sex.getChildCount(); i++) {}
                Toast.makeText(MainActivity.this, rd.getText(), Toast.LENGTH_SHORT).show();
            }
        });

        //progressBar
        progressBar = findViewById(R.id.progress);
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == 0x111) {
                    progressBar.setProgress(progress);
                } else {
                    Toast.makeText(MainActivity.this, "finish!", Toast.LENGTH_SHORT).show();
                    //progressBar.setVisibility(View.GONE);
                }
            }
        };
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    progress = doWork();
                    Message m = new Message();
                    if (progress < 100) {
                        m.what = 0x111;
                        handler.sendMessage(m);
                    } else {
                        m.what = 0x110;
                        handler.sendMessage(m);
                        break;
                    }
                }
            }
        }).start();

        //SeekBar调节屏幕亮度
        //判断当前系统的版本是否大于或等于6.0,符合条件将跳转开启系统设置权限的界面
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!Settings.System.canWrite(this)) {
                Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
                intent.setData(Uri.parse("package:" + getPackageName()));
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        }
        SeekBar seekBar = (SeekBar) findViewById(R.id.seekBar);
        seekBar.setProgress(getScreenBrightness(MainActivity.this));
        seekBar.setMax(255);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                Toast.makeText(MainActivity.this, "屏幕亮度" + progress, Toast.LENGTH_SHORT);
                setScreenBrightness(MainActivity.this, progress);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        //RatingBar
        RatingBar ratingbar = (RatingBar) findViewById(R.id.ratbar);
        int result = ratingbar.getProgress(); //获取进度
        float rating = ratingbar.getRating(); //获取等级
        float step = ratingbar.getStepSize(); //获取每次最少要改变多少个星级
        Log.i("星级评分条", "step=" + step + " result=" + result + " rating=" + rating);
    }

    //模拟耗时操作
    public int doWork() {
        progress += Math.random() * 10;
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return progress;
    }

    //取得屏幕亮度值
    public int getScreenBrightness(Context context) {
        int v = 0;
        ContentResolver resolver = context.getContentResolver();
        try {
            v = Settings.System.getInt(resolver, Settings.System.SCREEN_BRIGHTNESS);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return v;
    }

    //设置屏幕亮度值
    public void setScreenBrightness(Context context, int brightness) {
        brightness = brightness < 1 ? 1 : brightness;
        brightness = brightness > 255 ? 255 : brightness;
        Uri uri = Settings.System.getUriFor("screen_brightness");//获取设置系统屏幕亮度的uri
        Settings.System.putInt(context.getContentResolver(), "screen_brightness", brightness);//保存修改后的屏幕亮度值
        context.getContentResolver().notifyChange(uri, null);//更新亮度值
    }
}
全局设置 AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.qingshan.demo">
    <uses-permission android:name="android.permission.WRITE_SETTINGS"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

如果觉得文章对您有帮助,您可以对我进行打赏 ¥1.53 元(金额随机^_^,每次刷新金额不同)。

请使用支付宝扫码支付

留言评论

*称  呼:
*联系方式: 方便与您取得联系,推荐使用邮箱。
*内  容:

已有评论

暂无数据

上一篇:Android Fragment 中使用 WebView

下一篇:Android 常用组件-Spinner