編輯:關於android開發
上一章學習了AlertDialog,後來發現還有基於Layout文件的AlertDialog。可以自己排好位置,相對復雜一點。
先看看效果

圖中已經按布局文件排好位置了。
新加了個Layout文件:dialog_layout.xml
然後看看代碼:直接在上一章的基礎上改的代碼,好像有點多。
package com.fable.helloworld;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.GridView;
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;
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);
}
}
//=========================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();
}
}
}
Android View體系(六)從源碼解析Activity的構成
Android View體系(六)從源碼解析Activity的構成 相關文章 Android View體系(一)視圖坐標系 Android View體系(二)實現Vi
Android手機屏幕大小的獲取,android屏幕獲取
Android手機屏幕大小的獲取,android屏幕獲取package com.example.testactivity; import android.app.Act
android 嵌套的listview示例(可參考實現朋友圈評論)
android 嵌套的listview示例(可參考實現朋友圈評論) 最近在項目中用到listview中再嵌套一個listview,兩層也有監聽,都沒有問題。其實,主要
android GridLayout布局,androidgridlayout
android GridLayout布局,androidgridlayout android4.0版本後新增了一個GridLayout,它使用虛細線將布局劃分為行、列和