Android 關於Dialog彈出框

來源:互聯網
上載者:User

標籤:android   layout   

直接上:

實現步驟:

1.主介面activity_main.xml很簡單,一個按鈕

  

<RelativeLayout 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:background="@drawable/skin_background" >    <Button        android:id="@+id/show"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:gravity="center"        android:text="退出系統"        android:textSize="20sp" /></RelativeLayout>
2.彈出層樣式actionsheet.xml

  

<?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="wrap_content"    android:background="@color/transparent"    android:orientation="vertical"    android:padding="5dp" >    <TextView        android:id="@+id/title"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:background="@drawable/actionsheet_top_normal"        android:gravity="center"        android:text="@string/title"        android:textColor="#8F8F8F"        android:textSize="16sp" />    <TextView        android:id="@+id/content"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:background="@drawable/actionsheet_bottom_selector"        android:gravity="center"        android:text="@string/content"        android:textColor="#FD4A2E"        android:textSize="16sp" />    <TextView        android:id="@+id/cancel"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginTop="10dp"        android:background="@drawable/actionsheet_single_selector"        android:gravity="center"        android:text="@string/cancel"        android:textColor="#037BFF"        android:textSize="16sp" /></LinearLayout>
3.ActionSheet類

  

public class ActionSheet {public interface OnActionSheetSelected {void onClick(int whichButton);}private ActionSheet() {}public static Dialog showSheet(Context context, final OnActionSheetSelected actionSheetSelected,OnCancelListener cancelListener) {final Dialog dlg = new Dialog(context, R.style.ActionSheet);LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.actionsheet, null);final int cFullFillWidth = 10000;layout.setMinimumWidth(cFullFillWidth);TextView mContent = (TextView) layout.findViewById(R.id.content);TextView mCancel = (TextView) layout.findViewById(R.id.cancel);mContent.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubactionSheetSelected.onClick(0);dlg.dismiss();}});mCancel.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubactionSheetSelected.onClick(1);dlg.dismiss();}});Window w = dlg.getWindow();WindowManager.LayoutParams lp = w.getAttributes();lp.x = 0;final int cMakeBottom = 0;lp.y = cMakeBottom;lp.gravity = Gravity.CENTER;dlg.onWindowAttributesChanged(lp);dlg.setCanceledOnTouchOutside(false);if (cancelListener != null)dlg.setOnCancelListener(cancelListener);dlg.setContentView(layout);dlg.show();return dlg;}}
4.MainActivity

  

public class MainActivity extends Activity implements OnActionSheetSelected, OnCancelListener {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);findViewById(R.id.show).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubActionSheet.showSheet(MainActivity.this, MainActivity.this, MainActivity.this);}});}@Overridepublic void onClick(int whichButton) {// TODO Auto-generated method stubswitch (whichButton) {case 0:showToast("確定");break;case 1:showToast("取消");break;default:break;}}@Overridepublic void onCancel(DialogInterface dialog) {// TODO Auto-generated method stubshowToast("取消");}private void showToast(CharSequence charSequence) {Toast.makeText(this, charSequence, Toast.LENGTH_SHORT).show();}}

 源碼



   

Android 關於Dialog彈出框

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.