效果圖:
activity代碼:
package cn.com.chenzheng_java; import android.app.Activity; import android.app.Dialog; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.view.WindowManager; import android.view.View.OnClickListener; import android.view.ViewGroup.LayoutParams; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; /*** * @description 查看手機電池電量,這裏要注意監聽Intent.ACTION_BATTERY_CHANGED的廣播接收者,只能 * 在程序中顯示的註冊,不能再androiManifest.xml中進行靜態的註冊 * @author chenzheng_java * @since 2011/03/16 * */ public class BatteryActivity extends Activity implements OnClickListener{ // 當系統處於充電狀態或者電量發生改變時會廣播該action private static final String ACTION_NAME = Intent.ACTION_BATTERY_CHANGED; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.battery); Button button = (Button) findViewById(R.id.button1); // 注意哦,我們這裏的activity實現了View.OnClickListener接口 button.setOnClickListener(this); } // 聲明廣播接收者 BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String actionName = intent.getAction(); if(actionName.equals(ACTION_NAME)){ int level = intent.getIntExtra("level", 0); int scale = intent.getIntExtra("scale", 100); // Toast.makeText(BatteryActivity.this, "level="+level+" scale="+scale, Toast.LENGTH_LONG).show(); final Dialog dialog = new Dialog(BatteryActivity.this); dialog.setTitle("電量顯示"); // 加載xml佈局文件爲View對象 LayoutInflater flater = LayoutInflater.from(BatteryActivity.this); View view = flater.inflate(R.layout.battery2, null); dialog.setContentView(view,new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); /** * 獲取該dialog顯示的父窗口,然後設置當dialog顯示時,窗口內的其他部分設置爲模糊 * WindowManager.LayoutParams.FLAG_BLUR_BEHIND告訴我們,無論什麼對象顯示於前段, * 都會顯示在window的最上層 */ Window window = dialog.getWindow(); window.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND, WindowManager.LayoutParams.FLAG_BLUR_BEHIND); /** * 這裏注意findViewById()方法,默認的會去加載main.xml中的組件。如果你想加載x.xml中的id爲 * a的組件,那麼應該在前面加上setContentView這個佈局文件的對象,如這裏的dialog */ TextView textView = (TextView)dialog.findViewById(R.id.textView_message); textView.setText("目前的手機電量爲"+level*100/scale+"%"); Button button2 = (Button) dialog.findViewById(R.id.button_back); button2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); // 註銷廣播接收者 unregisterReceiver(broadcastReceiver); } }); dialog.show(); } } }; @Override public void onClick(View v) { IntentFilter filter = new IntentFilter(ACTION_NAME); registerReceiver(broadcastReceiver, filter); } }
battery2.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <TextView android:text="TextView" android:id="@+id/textView_message" android:textSize="10sp" android:gravity="center" android:padding="10px" android:layout_width="fill_parent" android:layout_height="wrap_content"></TextView> <Button android:id="@+id/button_back" android:layout_width="wrap_content" android:text="返回" android:layout_height="wrap_content"></Button> </LinearLayout>
battery.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <Button android:text="顯示電量" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> </LinearLayout>
前提 你必須會創建aspire項目,不會的請先看微服務新體驗之Aspire初體驗 Aspirate (Aspir8) Aspirate 是將aspire項目發佈到k8s集羣的工具 安裝aspirate dotnet tool install
安裝配置相關軟件 安裝 PowerShell 7 / Core dotnet tool install --global PowerShell 安裝 Visual Studio 擴展 Microsoft Child Process Deb
前言 今天給大家分享一個.NET開源(MIT License)、免費、跨平臺(適用於 Windows、MacOS 和 Linux )、使用簡單的面部識別庫:FaceRecognitionDotNet。 項目介紹 FaceRecogniti
本週刊由 Python貓 出品,精心篩選國內外的 250+ 信息源,爲你挑選最值得分享的文章、教程、開源項目、軟件工具、播客和視頻、熱門話題等內容。願景:幫助所有讀者精進 Python 技術,並增長職業和副業的收入。 本期週刊分享了 12
官方安裝指南 https://developer.hashicorp.com/terraform/install https://developer.hashicorp.com/terraform/intro/getting-sta
LQR 是一種優化控制方法,設計目標是找到一組控制輸入,使得線性系統的狀態軌跡儘可能地接近目標,同時使控制輸入儘可能小。其目標函數是一個二次型成本函數。 分爲以下幾個步驟: 1. 設系統動態方程爲: 其中x爲狀態量,u爲控制輸入,A和B爲
Most commonly used tags in HTML Last Updated : 08 Mar, 2024 Most commonly used tags in HTML refer to HTM
https://www.w3schools.com/css/css_math_functions.asp The CSS math functions allow mathematical expressions to be used
CSS tutorials (w3school) https://www.schoolsw3.com/css/index.php (Русский язык) https://www.w3schools.com/css/css_intro
https://www.w3schools.com/css/css_important.asp What is !important? The !important rule in CSS is used to add more imp
https://www.w3schools.com/css/css_website_layout.asp Website Layout A website is often divided into headers, menus, co
https://www.w3schools.com/css/css_form.asp The look of an HTML form can be greatly improved with CSS: <!DOCTYPE html>
https://www.w3schools.com/css/css_counters.asp CSS counters are "variables" maintained by CSS whose values can be inc
https://zhuanlan.zhihu.com/p/670589063 CSS Specificity(CSS 特異性)是一個用來決定當多個CSS規則應用於同一個元素時,哪個規則將優先應用的機制。 What is Specific
https://www.w3schools.com/css/css_units.asp CSS Units CSS has several different units for expressing a length. Many CS