• android 学习随笔十九(对话框、样式、主题、国际化 )


    1、对话框

      1 package com.itheima.dialog;
      2 
      3 import android.os.Bundle;
      4 import android.app.Activity;
      5 import android.app.AlertDialog;
      6 import android.app.AlertDialog.Builder;
      7 import android.app.ProgressDialog;
      8 import android.content.DialogInterface;
      9 import android.content.DialogInterface.OnClickListener;
     10 import android.content.DialogInterface.OnMultiChoiceClickListener;
     11 import android.view.Menu;
     12 import android.view.View;
     13 import android.widget.Toast;
     14 
     15 public class MainActivity extends Activity {
     16 
     17     @Override
     18     protected void onCreate(Bundle savedInstanceState) {
     19         super.onCreate(savedInstanceState);
     20         setContentView(R.layout.activity_main);
     21     }
     22 
     23 
     24     public void click1(View v){
     25         //创建对话框创建器
     26         AlertDialog.Builder builder = new Builder(this);
     27         //设置图标
     28         builder.setIcon(android.R.drawable.ic_dialog_alert);
     29         //设置标题
     30         builder.setTitle("警告");
     31         //设置内容
     32         builder.setMessage("欲练此功必先自宫,春晓你确定要自宫吗");
     33         
     34         //设置确定按钮
     35         builder.setPositiveButton("确定", new OnClickListener() {
     36             
     37             @Override
     38             public void onClick(DialogInterface dialog, int which) {
     39                 Toast.makeText(MainActivity.this, "自宫完成,谢谢使用", 0).show();
     40                 
     41             }
     42         });
     43         //设置取消按钮
     44         builder.setNegativeButton("取消", new OnClickListener() {
     45             
     46             @Override
     47             public void onClick(DialogInterface dialog, int which) {
     48                 Toast.makeText(MainActivity.this, "若不自宫,一定不成功", 0).show();
     49                 
     50             }
     51         });
     52         
     53         //显示对话框
     54         AlertDialog ad = builder.create();
     55         ad.show();
     56     }
     57     
     58     public void click2(View v){
     59         //创建对话框创建器
     60         AlertDialog.Builder builder = new Builder(this);
     61         //设置图标
     62         builder.setIcon(android.R.drawable.ic_dialog_alert);
     63         //设置标题
     64         builder.setTitle("选择您的家乡");
     65         
     66         final String[] items = new String[]{
     67                 "火星",
     68                 "塞伯坦",
     69                 "氪星",
     70                 "M78星云",
     71         };
     72         
     73         //设置单选选项
     74         builder.setSingleChoiceItems(items, 1, new OnClickListener() {
     75             
     76             //dialog:触发此方法执行的那个对话框
     77             @Override
     78             public void onClick(DialogInterface dialog, int which) {
     79                 Toast.makeText(MainActivity.this, items[which], 0).show();
     80                 //对话框消失
     81                 dialog.dismiss();
     82                 
     83             }
     84         });
     85         
     86         //显示对话框
     87         builder.show();
     88     }
     89     
     90     public void click3(View v){
     91         //创建对话框创建器
     92         AlertDialog.Builder builder = new Builder(this);
     93         //设置图标
     94         builder.setIcon(android.R.drawable.ic_dialog_alert);
     95         //设置标题
     96         builder.setTitle("选择您需要的武器");
     97         
     98         final String[] items = new String[]{
     99                 "板凳",
    100                 "啤酒瓶",
    101                 "开山刀",
    102                 "AK-47",
    103                 "爱国者",
    104                 "唐晓平"
    105         };
    106         
    107         final boolean[] checkedItems = new boolean[]{
    108                 true,
    109                 false,
    110                 false,
    111                 false,
    112                 false,
    113                 true
    114         };
    115         
    116         //设置多选选项
    117         builder.setMultiChoiceItems(items, checkedItems, new OnMultiChoiceClickListener() {
    118             
    119             @Override
    120             public void onClick(DialogInterface dialog, int which, boolean isChecked) {
    121                 //记录用户的选择
    122                 checkedItems[which] = isChecked;
    123             }
    124         });
    125         builder.setPositiveButton("确定", new OnClickListener() {
    126             
    127             @Override
    128             public void onClick(DialogInterface dialog, int which) {
    129                 String text = "";
    130                 for (int i = 0; i < checkedItems.length; i++) {
    131                     text += checkedItems[i]? items[i] + ",": "";
    132                 }
    133                 Toast.makeText(MainActivity.this, text, 0).show();
    134                 
    135             }
    136         });
    137         
    138         builder.show();
    139     }
    140     
    141     public void click4(View v){
    142         //创建进度条对话框
    143         final ProgressDialog dialog = new ProgressDialog(this);
    144         dialog.setIcon(android.R.drawable.ic_input_add);
    145         dialog.setTitle("正在自宫中,请稍候...");
    146         dialog.setMax(100);
    147         dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    148         Thread t = new Thread(){
    149             public void run() {
    150                 for (int i = 0; i <= 100; i++) {
    151                     dialog.setProgress(i);
    152                     try {
    153                         sleep(20);
    154                     } catch (InterruptedException e) {
    155                         // TODO Auto-generated catch block
    156                         e.printStackTrace();
    157                     }
    158                 }
    159                 //自宫完成,进度消失
    160                 dialog.dismiss();
    161             }
    162         };
    163         t.start();
    164         dialog.show();
    165     }
    166 }
    View Code
     1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:paddingBottom="@dimen/activity_vertical_margin"
     6     android:paddingLeft="@dimen/activity_horizontal_margin"
     7     android:paddingRight="@dimen/activity_horizontal_margin"
     8     android:paddingTop="@dimen/activity_vertical_margin"
     9     tools:context=".MainActivity" 
    10     android:orientation="vertical"
    11     >
    12 
    13     <Button
    14         android:layout_width="wrap_content"
    15         android:layout_height="wrap_content"
    16         android:text="确定取消对话框" 
    17         android:onClick="click1"
    18         />
    19     <Button
    20         android:layout_width="wrap_content"
    21         android:layout_height="wrap_content"
    22         android:text="单选对话框" 
    23         android:onClick="click2"
    24         />
    25     <Button
    26         android:layout_width="wrap_content"
    27         android:layout_height="wrap_content"
    28         android:text="多选对话框" 
    29         android:onClick="click3"
    30         />
    31     <Button
    32         android:layout_width="wrap_content"
    33         android:layout_height="wrap_content"
    34         android:text="进度条对话框" 
    35         android:onClick="click4"
    36         />
    37 
    38 </LinearLayout>
    View Code

    2、样式和主题

     1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:paddingBottom="@dimen/activity_vertical_margin"
     6     android:paddingLeft="@dimen/activity_horizontal_margin"
     7     android:paddingRight="@dimen/activity_horizontal_margin"
     8     android:paddingTop="@dimen/activity_vertical_margin"
     9     tools:context=".MainActivity" 
    10     android:orientation="vertical"
    11     >
    12 
    13     <TextView
    14         android:text="@string/hello_world" 
    15         style="@style/lakesouthstyle"
    16         />
    17     <TextView
    18         android:text="@string/hello_world" 
    19         style="@style/chunxiaostyle"
    20         />
    21     <TextView
    22         android:text="@string/hello_world" 
    23         style="@style/lakesouthstyle.miaorunstyle"
    24         />
    25     <TextView
    26         android:text="@string/hello_world" 
    27         style="@style/lakesouthstyle"
    28         />
    29 
    30 </LinearLayout>
    View Code

    在values目录下定义样式,主题和样式定义相同,主题应用在清单文件中(主题用于Activity),样式应用在布局文件中

     1 <resources xmlns:android="http://schemas.android.com/apk/res/android">
     2 
     3     <!--
     4         Base application theme, dependent on API level. This theme is replaced
     5         by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
     6     -->
     7     <style name="AppBaseTheme" parent="android:Theme.Light">
     8         <!--
     9             Theme customizations available in newer API levels can go in
    10             res/values-vXX/styles.xml, while customizations related to
    11             backward-compatibility can go here.
    12         -->
    13     </style>
    14 
    15     <!-- Application theme. -->
    16     <style name="AppTheme" parent="AppBaseTheme">
    17         <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    18         <item name="android:background">#ff0000</item>
    19     </style>
    20     
    21     <style name="lakesouthstyle">
    22         <item name="android:layout_width">wrap_content</item>
    23         <item name="android:layout_height">wrap_content</item>
    24         <item name="android:textSize">30sp</item>
    25         <item name="android:textColor">#00ff00</item>
    26     </style>
    27     <style name="chunxiaostyle" parent="lakesouthstyle">
    28         <item name="android:textSize">20sp</item>
    29     </style>
    30     <style name="lakesouthstyle.miaorunstyle">
    31         <item name="android:textColor">#ff0000</item>
    32     </style>
    33 
    34 </resources>
    View Code

    3、国际化  

    根据语言需求定义values,values_zh,values_it,drawable-en-rUS(找出国家语言简写)等定义资源。

     文本国际化

    1 <?xml version="1.0" encoding="utf-8"?>
    2 <resources>
    3 
    4     <string name="app_name">11_国际化</string>
    5     <string name="action_settings">Settings</string>
    6     <string name="hello_world">这是英文的你好世界</string>
    7 
    8 </resources>
    values,英文
    1 <?xml version="1.0" encoding="utf-8"?>
    2 <resources>
    3 
    4     <string name="app_name">11_国际化</string>
    5     <string name="action_settings">Settings</string>
    6     <string name="hello_world">这是中文的你好世界</string>
    7 
    8 </resources>
    values-zh
    1 <?xml version="1.0" encoding="utf-8"?>
    2 <resources>
    3 
    4     <string name="app_name">11_国际化</string>
    5     <string name="action_settings">Settings</string>
    6     <string name="hello_world">这是意大利文的你好世界</string>
    7 
    8 </resources>
    values-it

    图片也可以国际化,定义drawable-en-rGB、drawable-en-rUS等

  • 相关阅读:
    Quote comes from C. Joybell C.
    Operating System: Three Easy Pieces --- LDE (Note)
    Operating System: Three Easy Pieces --- Pthread Locks (Note)
    Operating System: Three Easy Pieces --- Locks (Note)
    Modern Operating System --- Multiple Processor Systems
    Modern Operating System --- Power Management (CPU)
    Modern Operating System --- Power Management (Hard Disk)
    ls指定日期和时间输出格式
    python StringIO
    PING命令入门详解
  • 原文地址:https://www.cnblogs.com/ecollab/p/5919756.html
Copyright © 2020-2023  润新知