android中的常見對話方塊

來源:互聯網
上載者:User

標籤:nbsp   activity   朋友   create   attribute   sch   fill   ica   print   

在android中對話方塊是一種常見的操作,常見的對話方塊有下面幾種:


以下是xml布局檔案:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context=".MainActivity" >    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:onClick="click1"        android:text="顯示通知對話方塊" />    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:onClick="click2"        android:text="顯示單選對話方塊" />    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:onClick="click3"        android:text="顯示多選對話方塊" />    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:onClick="click4"        android:text="顯示運行進度對話方塊" />    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:onClick="click5"        android:text="顯示運行進度條對話方塊" /></LinearLayout>

實現代碼:

顯示對話方塊

public void click1(View view) {AlertDialog.Builder builder = new Builder(this);builder.setTitle("顯示對話方塊");builder.setIcon(R.drawable.ic_launcher);builder.setMessage("你確定嗎?");builder.setPositiveButton("確定", new OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Toast.makeText(MainActivity.this, "確定被點擊了", Toast.LENGTH_LONG).show();}});builder.setNegativeButton("取消", new OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub}});builder.show();}

顯示效果:


顯示單選對話方塊

public void click2(View view) {AlertDialog.Builder builder = new Builder(this);builder.setTitle("單選對話方塊");final String[] items = new String[] { "條目1", "條目2", "條目3" };builder.setSingleChoiceItems(items, 1, new OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubToast.makeText(MainActivity.this, items[which] + "被選中了",Toast.LENGTH_LONG).show();dialog.dismiss();}});builder.show();}

顯示效果:


顯示多選對話方塊

public void click3(View view) {AlertDialog.Builder builder = new Builder(this);builder.setTitle("顯示多選對話方塊");final String[] items = new String[] { "條目1", "條目2", "條目3", "條目4" };builder.setMultiChoiceItems(items, new boolean[] { true, false, false,true }, new OnMultiChoiceClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which,boolean isChecked) {// TODO Auto-generated method stubToast.makeText(MainActivity.this, items[which] + isChecked,Toast.LENGTH_LONG).show();}});builder.setNegativeButton("取消", new OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub}});builder.show();}

顯示效果:

顯示運行進度對話方塊

public void click4(View view) {ProgressDialog dialog = new ProgressDialog(this);dialog.setTitle("提醒");dialog.setMessage("正在上傳中");dialog.show();}

顯示效果:


顯示運行進度條對話方塊

public void click5(View view) {final ProgressDialog dialog = new ProgressDialog(this);dialog.setTitle("載入進度");dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);dialog.setMax(100);dialog.show();new Thread() {public void run() {for (int i = 0; i < 100; i++) {dialog.setProgress(i);try {Thread.sleep(100);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}dialog.dismiss();};}.start();}

顯示效果:


最後補充一點假設是自己定義dialog的話假設你用的是

dialog.setContentView(R.layout.dialog_wait);那麼就沒有什麼問題。

假設你是通過inflater來建立一個view

 dialog.setContentView(view)

這樣的方式來設定view的話。會遇到設定的dialog大小無效,view佔滿整個螢幕的情況。

這個時候就須要在代碼中設定view的大小。

 WindowManager.LayoutParams params=dialog.getWindow().getAttributes();        params.width=400;        params.height=400;        dialog.getWindow().setAttributes(params);


自己定義對話方塊顯示在底部

看到非常多應用在分享到第三方平台的時候。螢幕底部顯示一個對話方塊


這樣就須要自己定義Dialog的屬性了,讓dialog顯示在螢幕的底部。

    public static void showShareDialog(final Activity activity) {        final AlertDialog dialog = new AlertDialog.Builder(activity).create();        dialog.show();        View view = View.inflate(activity, R.layout.view_dialog_share, null);        TextView tvWeixinFriend = (TextView) view.findViewById(R.id.share_wexin);        tvWeixinFriend.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                dialog.dismiss();                ShareManager.getInstance().shareToWeixinFriend(activity);            }        });        TextView tvWeixinCircle = (TextView) view.findViewById(R.id.share_pyq);        tvWeixinCircle.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                dialog.dismiss();                ShareManager.getInstance().shareToWeixinCircle(activity);            }        });        TextView tvWeibo = (TextView) view.findViewById(R.id.share_weibo);        tvWeibo.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                dialog.dismiss();                ShareManager.getInstance().shareToWeibo(activity);            }        });        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();        lp.width = ViewGroup.LayoutParams.MATCH_PARENT;        lp.gravity = Gravity.BOTTOM;        dialog.getWindow().setAttributes(lp);        dialog.setContentView(view);            }

以下是

view_dialog_share
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_alignParentBottom="true"    android:background="@color/white"    android:orientation="horizontal"    android:paddingBottom="16dp"    android:paddingTop="16dp">    <TextView        android:id="@+id/share_wexin"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_weight="1"        android:drawablePadding="4dp"        android:drawableTop="@drawable/icon_share_weixin_friend"        android:gravity="center_horizontal"        android:text="好友" />    <TextView        android:id="@+id/share_pyq"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_weight="1"        android:drawablePadding="4dp"        android:drawableTop="@drawable/icon_share_weixin_circle"        android:gravity="center_horizontal"        android:text="朋友圈" />    <TextView        android:id="@+id/share_weibo"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_weight="1"        android:drawablePadding="4dp"        android:drawableTop="@drawable/icon_share_weibo"        android:gravity="center_horizontal"        android:text="微博" /></LinearLayout>





android中的常見對話方塊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.