編輯:關於android開發
上兩章都說了非阻塞的對話框,今天說一下阻塞的對話框--PopupWindow
那麼什麼是阻塞什麼是非阻塞呢?PopupWindow和AlertDialog有什麼不同呢?
先說AlertDialog,彈出來之後,背面會變灰,並沒有阻塞後台的進程,如果沒特殊控制,點擊後面灰暗處,彈框會消失掉的。
至於PopupWindow,則是彈出來,後面沒有任何變化,並且阻塞該應用的進程,如果一直沒退出,應用匯一直等待,點擊後面也是沒有反應的。
不知道為什麼現在上傳不了圖,就不上傳了,其實跟AlertDialog一樣。
還是繼續使用之前的代碼來擴充
來看看怎麼實現:
新增了一個popup_window.xml
聲明了一個標題,一個對話框,兩個按鈕。
然後看實現代碼,看過AlertDialog的可以調到下面的代碼去。
package com.fable.helloworld;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.PopupWindow;
import android.widget.SimpleAdapter;
import java.util.*;
public class HelloWorldActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hello_world); //設置主布局文件
GridView gridview = (GridView) findViewById(R.id.gridview);
//創造數據來源
ArrayList> images = new ArrayList>();
for(int i=1;i<10;i++)
{
String imageName = "";
switch(i)
{
case 1:
imageName = "AlertDialog";//普通的AlertDialog
break;
case 2:
imageName = "AlertDialog2";//基於布局的AlertDialog
break;
case 3:
imageName = "PopupWindow";//阻塞對話框
break;
default:
imageName = "app"+String.valueOf(i);
}
HashMap map = new HashMap();
map.put("ItemImage", R.drawable.ic_launcher);//添加圖像資源的ID,標識符,值
map.put("ItemText", imageName);//按序號做ItemText,標識符,值
images.add(map);
}
//把數據傳入適配器,轉換成布局需要的數據
SimpleAdapter simpleAdapter = new SimpleAdapter(this, //上下文為當前Activity
images,//數據來源
R.layout.my_list_item,//每一項的布局的XML實現
new String[] {"ItemImage","ItemText"},//動態數組與ImageItem對應的子項
new int[] {R.id.ItemImage,R.id.ItemText}); //ImageItem的XML文件裡面的一個ImageView,兩個TextView ID
//添加並且顯示
gridview.setAdapter(simpleAdapter);
//添加消息處理
gridview.setOnItemClickListener(new ItemClickListener());
}
//當AdapterView被單擊(觸摸屏或者鍵盤),則返回的Item單擊事件
class ItemClickListener implements OnItemClickListener
{
public void onItemClick(AdapterView arg0,//父視圖
View arg1,//當前視圖
int arg2,//點擊的位置
long arg3//id
) {
HashMap item = (HashMap) arg0.getItemAtPosition(arg2); //獲取點擊的item
//setTitle((String)item.get("ItemText")); //這個只是把標題改一改,
String itemStr = (String)item.get("ItemText");
if(itemStr.equals("AlertDialog")){
showDialog(HelloWorldActivity.this, itemStr);
}
else if (itemStr.equals("AlertDialog2"))
{
showDialogLayout(HelloWorldActivity.this);
}
else if( itemStr.equals("PopupWindow"))
{
showPopupWindow(HelloWorldActivity.this, arg1);
}
}
//=========================AlertDialog====================================================
private void showDialog(Context context, String itemStr) {
//AlertAialog的構造函數是protected的,只能通過Builder函數來構建一個新的對象
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setIcon(R.drawable.ic_launcher); //設置圖標
builder.setTitle("我是標題"); //設置標題
builder.setMessage("這裡是內容啊啊啊啊!!!");//設置內容
builder.setPositiveButton("Button1", //確認按鈕
new DialogInterface.OnClickListener() {//為了方便,不顯式聲明一個類了
public void onClick(DialogInterface dialog, int whichButton) {
setTitle("點擊了對話框上的Button1");
}
});
builder.setNeutralButton("Button2", //中性按鈕
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
setTitle("點擊了對話框上的Button2");
}
});
builder.setNegativeButton("Button3", //否認按鈕
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
setTitle("點擊了對話框上的Button3");
}
});
builder.show(); //顯式這個對話框
}
//===================基於Layout的AlertDialog================================================
private void showDialogLayout(Context context) {
//LayoutInflater的作用是用來動態加載Layout文件的
LayoutInflater inflater = LayoutInflater.from(context);
final View textEntryView = inflater.inflate( R.layout.dialog_layout, null);//動態加載Layout文件
final EditText edtInput=(EditText)textEntryView.findViewById(R.id.edtInput);//加載之後可以找到其中的控件了
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setCancelable(false);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("Title");
builder.setView(textEntryView);
builder.setPositiveButton("確認", //這裡又手動加入了按鈕,可以看出,可以混著用的
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
setTitle(edtInput.getText());
}
});
builder.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
setTitle("");
}
});
builder.show();
}
//===================PopupWindow================================================
private void showPopupWindow(Context context,View parent) {
//LayoutInflater的作用是用來動態加載Layout文件的
LayoutInflater inflater = LayoutInflater.from(context);
final View popupView = inflater.inflate( R.layout.popup_window, null);//動態加載Layout文件
final PopupWindow pWindow = new PopupWindow(popupView,200,200,true);//需要填寫寬高,否則顯示不了
final Button button=(Button)popupView.findViewById(R.id.BtnOK);//加載之後可以找到其中的控件了
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
//設置文本框內容
EditText edtUsername=(EditText)popupView.findViewById(R.id.data_edit);
edtUsername.setText("關注微信:傳說之路");
}
});
//Cancel按鈕及其處理事件
Button btnCancel=(Button)popupView.findViewById(R.id.BtnCancel);
btnCancel.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
pWindow.dismiss();//關閉
}
});
//顯示popupWindow對話框
pWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
}
}
}
自用工程教程(一)建立開發環境與HelloWorld,自用helloworld
自用工程教程(一)建立開發環境與HelloWorld,自用helloworld從今天開始,我們將在老師的帶領下嘗試做一個Android平台移動端實現捕獲網絡數據包功能的A
記CBS一次動人心魄的數據保衛戰
記CBS一次動人心魄的數據保衛戰接觸分布式存儲已經有一年多的時間了,首次遇到存儲側三份數據都有異常的情況,三份數據異常意味著客戶數據的丟失,這個對雲存儲來講是致命的打擊。
Android開啟多進程
Android開啟多進程 1. 為何要開啟多進程 為何開啟android應用要開啟多進程,主要有以下幾點: 單進程所分配的內存不夠,需要更多的內存。在早期android系
ActionBar效果圖,功能一覽,actionbar效果圖
ActionBar效果圖,功能一覽,actionbar效果圖一.概述 1.App icon 應用的圖標,左側帶應用相當於back返回鍵 2.ViewContr