a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 系統 > Android > 正文

Android入門之PopupWindow用法實例解析

2020-04-11 11:46:38
字體:
來源:轉載
供稿:網友

本文實例介紹一下PopupWindow對話框。PopupWindow是阻塞對話框,只有在外部線程 或者 PopupWindow本身做退出操作才可以執行。PopupWindow完全依賴Layout做外觀,在常見的開發中,PopupWindow應該會與AlertDialog?;煊?。

先貼出本例中運行的結果圖:

main.xml的源碼如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  ><Button android:id="@+id/Button01" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="PopupWindow演示"></Button></LinearLayout>

下圖所示是PopupWindow彈出的截圖,這里的PopupWindow是個登錄框,點“確定”則自動填寫,點“取消”則關閉PopupWindow。

 

popupwindow.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="wrap_content" android:orientation="vertical" android:background="#000000"> <TextView android:id="@+id/username_view" android:layout_height="wrap_content" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:text="用戶名" android:textAppearance="?android:attr/textAppearanceMedium" android:layout_width="fill_parent"/> <EditText android:id="@+id/username_edit" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:capitalize="none" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/password_view" android:layout_height="wrap_content" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:text="密碼" android:textAppearance="?android:attr/textAppearanceMedium" android:layout_width="fill_parent"/> <EditText android:id="@+id/password_edit" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:capitalize="none" android:password="true" android:textAppearance="?android:attr/textAppearanceMedium" /><LinearLayout android:id="@+id/LinearLayout01" android:layout_height="wrap_content" android:layout_width="fill_parent" android:gravity="center"><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/BtnOK" android:layout_weight="100" android:text="確定"></Button><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="100" android:text="取消" android:id="@+id/BtnCancel"></Button></LinearLayout></LinearLayout>

接下來是Java程序源碼:

package com.testAlertDialog;import android.app.Activity;import android.app.AlertDialog;import android.content.Context;import android.content.DialogInterface;import android.os.Bundle;import android.text.Editable;import android.view.Gravity;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.PopupWindow;public class testAlertDialog extends Activity { Button btnPopupWindow;  /** Called when the activity is first created. */  @Override  public void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.main);    //定義按鈕    btnPopupWindow=(Button)this.findViewById(R.id.Button01);    btnPopupWindow.setOnClickListener(new ClickEvent());  }      //統一處理按鍵事件  class ClickEvent implements OnClickListener{   @Override   public void onClick(View v) {   // TODO Auto-generated method stub   if(v==btnPopupWindow)   {    showPopupWindow(testAlertDialog.this,     testAlertDialog.this.findViewById(R.id.Button01));   }   }  } public void showPopupWindow(Context context,View parent){ LayoutInflater inflater = (LayoutInflater)      context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);     final View vPopupWindow=inflater.inflate(R.layout.popupwindow, null, false);   final PopupWindow pw= new PopupWindow(vPopupWindow,300,300,true);   //OK按鈕及其處理事件    Button btnOK=(Button)vPopupWindow.findViewById(R.id.BtnOK);    btnOK.setOnClickListener(new OnClickListener(){  @Override  public void onClick(View v) {  //設置文本框內容    EditText edtUsername=(EditText)vPopupWindow.findViewById(R.id.username_edit);    edtUsername.setText("username");    EditText edtPassword=(EditText)vPopupWindow.findViewById(R.id.password_edit);    edtPassword.setText("password");  }    });       //Cancel按鈕及其處理事件    Button btnCancel=(Button)vPopupWindow.findViewById(R.id.BtnCancel);    btnCancel.setOnClickListener(new OnClickListener(){  @Override  public void onClick(View v) {  pw.dismiss();//關閉  }    });    //顯示popupWindow對話框   pw.showAtLocation(parent, Gravity.CENTER, 0, 0); }}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 亚洲精品做爰大胆视频在线 | 亚洲成a | 国产精品一区欧美 | 黄色av毛片 | 国产精品九九九 | 久久人体 | 精品国产成人 | 久久久精品欧美 | 在线视频亚洲 | 亚洲精品久久久久久一区二区 | 伊人网亚洲 | 在线精品亚洲欧美日韩国产 | 国产成人精品免费 | 久久99国产一区二区三区 | 久久综合一区二区三区 | 国产精品一二三区 | 色呦呦在线看 | 国产精品一区二区在线观看 | 99在线免费视频 | 不卡成人| 精品免费 | 在线视频福利 | 国产韩国精品一区二区三区 | 午夜精品一区二区三区在线观看 | 天天干人人插 | 国产精品人人做人人爽人人添 | www.9191| 天天草天天干 | 男男gay腐片h大尺度 | 一级在线观看 | 天天干天天插 | 日韩在线无 | 日本h在线观看 | 国产精品美女在线观看直播 | 久久久成人精品视频 | 精品久久久久久亚洲精品 | 午夜精品久久久久久99热软件 | 国产小视频一区二区 | 中文字幕欧美在线观看 | 欧美日韩在线免费观看 | 午夜精品一区二区三区在线观看 |