Android界面設計之對話框——定制Toast、AlertDialog


一、概述

  在界面設計中需要根據用戶操作顯示提示信息、出錯信息等,就要用到對話框。Android實現提示信息顯示常用有兩種方式

  1、Toast

  2、AlertDialog 

二、Toast

  Android中用來顯示顯示信息的一種機制,屬於輕量級消息開發中使用頻率很高。其特點

    1、 不接受用戶操作,沒有焦點

    2、 顯示的時間有限,過一定的時間就會自動消失。

  使用Toast顯示信息非常簡單,操作如下:

Toast toast=Toast.makeText(this, “數據加載完成”, Toast.LENGTH_LONG);//創建Toast

toast.show();//顯示消息

  普通的Toast外觀很簡單,我們也可以根據需要定制Toast,如新聞頭條中實現收藏和取消收藏的信息提示,如圖所示效果

 

  定制Toast主要有兩個方面 

    1、 定制Toast的外觀

    2、 設置Toast在屏幕位置

  實現定制Toast關鍵代碼如下:

Toast  toast = Toast.makeText(context, msg, Toast.LENGTH_LONG); //創建Toast
int offsetX = 0;
int offsetY = 0;
toast.setGravity(Gravity.BOTTOM, offsetX, offsetY);//設置位置
LinearLayout      ll=LayoutInflater.from(context).inflate(R.Layout.custom_toast,null);
toast.setView(ll);//設置外觀
toast.show();

  上述案例實現完成代碼如下:

1CustomToastActivity:

private Button  btCollect;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.custom_toast_activity);
        initView();
    }
    private void initView(){
        btCollect=(Button)super.findViewById(R.id.btCollect);
        btCollect.setOnClickListener(this);
    }

    private void showToast(String txt){
        Toast tast=Toast.makeText(this, txt, Toast.LENGTH_LONG);
        tast.setGravity(Gravity.CENTER, 0, 0);
        View view=LayoutInflater.from(this).inflate(R.layout.custom_toast, null);
        TextView tvMsg=(TextView)view.findViewById(R.id.tvMsg);
        tvMsg.setText(txt);
        tast.setView(view);
        tast.show();        
    }
    public void onClick(View view) {
        String txtCollect=btCollect.getText().toString();
        if(txtCollect.equals("收藏")){
            btCollect.setText("取消收藏");
            showToast("收藏成功");
        }else{
            btCollect.setText("收藏");
            showToast("取消收藏");
        }
    }

2 、custom_toast.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:padding="10dp"
    android:layout_height="match_parent"  android:background="@drawable/toast_shape" >
    <TextView
        android:id="@+id/tvImg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="60sp"
        android:text="★"
        android:textColor="@color/white"
         android:layout_centerHorizontal="true"
        >
    </TextView>
        
    <TextView
        android:id="@+id/tvMsg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="28sp"
        android:text="收藏成功"
        android:textColor="@color/white"
        android:layout_below="@+id/tvImg"
        android:layout_centerHorizontal="true"
        />

</RelativeLayout>

3、 toast_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="#000000"></solid>
       <corners android:radius="10dp"/>
</shape>
三、對話框

1、AlertDialog  

  簡單的一種對話框,主要的目的是為用戶顯示一條警告信息,通過AlertDialog.Builder產生AlertDialog,主要代碼如下:

Dialog dialog = new AlertDialog.Builder(this)
            .setTitle("對話框")         // 設置標題
            .setMessage("顯示提示信息。")     // 顯示信息
            .setIcon(R.drawable.pic_m)     // 設置顯示的Icon
setPositiveButton("確定", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
})    //設置按鈕
.create();             // 創建Dialog
dialog.show();    // 顯示對話框

其他主要方法:
setView : 給對話框設置自定義樣式 

setItems :設置對話框要顯示的一個list,一般用於顯示幾個命令時 

setSingleChoiceItems:用來設置對話框顯示一系列的單選框 

setMultiChoiceItems :用來設置對話框顯示一系列的復選框 

setNeutralButton    :普通按鈕

setPositiveButton   :給對話框添加"Yes"按鈕
setNegativeButton :對話框添加"No"按鈕 

create : 創建對話框
show :顯示對話框

2、定制AlertDialog 

  主要使用getWindow().setContentView()設置外觀,如實現退出應用對話框

 

  Activity——CustomAlertDialog代碼:

private void showExitDialog(){
        final AlertDialog exitDlg=new AlertDialog.Builder(this).create();
        exitDlg.show();//在添加內容之前執行
        Window win=exitDlg.getWindow();
  // 設置窗口的內容頁面,為exit_dlg_layout.xml文件中定義view內容
        win.setContentView(R.layout.exit_dlg_layout);
        TextView tvMsg=(TextView)win.findViewById(R.id.tvMsg);
        tvMsg.setText("要退出應用嗎?");
        Button btOk=(Button)win.findViewById(R.id.btOk);
        Button btCancel=(Button)win.findViewById(R.id.btCancel);
         // 為確認按鈕添加事件,執行退出應用操作
        btCancel.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                exitDlg.dismiss();// 關閉對話框
            }
        });
        btOk.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                finish();
                exitDlg.dismiss();// 關閉對話框
            }
        });
    }
    public void onBackPressed(){//按回退鍵調用
        showExitDialog();
    }

   exit_dlg_layout.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" android:padding="10dp">
    <LinearLayout android:layout_width="match_parent"
    android:layout_height="wrap_content" android:orientation="vertical" 
    android:background="@drawable/exit_dlg_shape"
    android:padding="5dp">
        <TextView 
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:gravity="center"
        android:textSize="20sp"
        android:text="溫馨提示"
           android:textColor="#000000" />
    <LinearLayout android:layout_width="match_parent"
   android:layout_height="100dp" android:orientation="vertical" android:background="#ffffff">
             <TextView 
                 android:id="@+id/tvMsg"
                 android:layout_width="match_parent"
                 android:layout_height="match_parent" 
                 android:gravity="center_vertical"
                 android:padding="5dp"/>
        </LinearLayout>
       <LinearLayout  
            android:layout_width="match_parent"
                    android:padding="5dp"
    android:layout_height="40dp" android:orientation="horizontal">
            <Button  
                android:id="@+id/btOk"
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:text="確定"
                android:layout_weight="1"
                android:background="@drawable/comm_shop_detail_top_green"
                android:layout_marginRight="2dp"    />
                 <Button  
                android:id="@+id/btCancel"
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:text="取消"
                android:layout_weight="1"
                android:background="@drawable/comm_shop_detail_top_black"
                />
        </LinearLayout>
    </LinearLayout>

</RelativeLayout>

 

作者: 傑瑞教育
出處: http://www.cnblogs.com/jerehedu/ 
本文版權歸煙台傑瑞教育科技有限公司和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM