編輯:關於Android編程
Android中經常會使用到Dialog(彈出框效果),而且功能非常強大,可以模擬出N種彈出框效果。如圖所示:
下面將通過一個小實例,來像大家展示Android中功能強大的Dialog。代碼都寫了詳細的注釋,讀者不妨試著手動去敲。
當然,由於時間的關系,還有個別功能沒有完成,先有的幾個展示效果中也可能有bug,代碼也有很多需要優化的地方。望大家熱心指出。
下面是代碼(有點長哦):
復制代碼 代碼如下:
package com.chaoyang.activity;
import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
final String[] items={"張三","李四","王五","趙六","阿貓","阿狗"};
int mSingleChoiceID = -1;
ArrayList <Integer>MultiChoiceID = new ArrayList <Integer>();
private ProgressDialog mProgressDialog = null;
private static final int MAX_PROGRESS = 100;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/*START 設置那些按鈕的點擊監聽對象*/
ButtonOnclikListen listen =new ButtonOnclikListen();
Button btnConfim =(Button)this.findViewById(R.id.btnConfim);
Button btnCheck =(Button)this.findViewById(R.id.btnCheck);
Button btnCustom =(Button)this.findViewById(R.id.btnCustom);
Button BtnList =(Button)this.findViewById(R.id.BtnList);
Button btnMore =(Button)this.findViewById(R.id.btnMore);
Button BtnProgressbar =(Button)this.findViewById(R.id.BtnProgressbar);
Button btnRead =(Button)this.findViewById(R.id.btnRead);
Button btnRidio =(Button)this.findViewById(R.id.btnRidio);
btnConfim.setOnClickListener(listen);
btnCheck.setOnClickListener(listen);
btnConfim.setOnClickListener(listen);
BtnList.setOnClickListener(listen);
btnMore.setOnClickListener(listen);
BtnProgressbar.setOnClickListener(listen);
btnRead.setOnClickListener(listen);
btnRidio.setOnClickListener(listen);
/* END */
}
//定義一個公共方法,彈出消息
private void showDialogMSG(String msg) {
new AlertDialog.Builder(MainActivity.this)
.setMessage(msg)
.show();
}
//定義一個內部類,繼承至OnClickListener,專門來處理按鈕點擊事件(面向對象,靈活)
private final class ButtonOnclikListen implements View.OnClickListener
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Button button =(Button) v;//接收被點擊的Button對象
AlertDialog.Builder builder =new AlertDialog.Builder(MainActivity.this);//定義一個彈出框對象
builder.setIcon(R.drawable.aa);//設置彈出框的ICON
switch (v.getId())
{
case R.id.btnConfim :
builder.setTitle("確定提交嗎?");//設置彈出框的標題
builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
showDialogMSG("您選擇了確定");
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
showDialogMSG("您選擇了取消");
}
});
break;
case R.id.btnMore:
builder.setTitle("評價");//標題
builder.setMessage("說說李孝利在你心裡的印象");//彈出框消息
builder.setPositiveButton("性感",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
showDialogMSG("您覺得她很性感");
}
});
builder.setNeutralButton("漂亮", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
showDialogMSG("您覺得她很漂亮");
}
});
builder.setNegativeButton("風騷", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
showDialogMSG("您覺得她很風騷");
}
});
break;
case R.id.BtnList :
builder.setTitle("列表框");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
showDialogMSG("您選擇了ID為"+which+"的人,名字叫:"+items[which]);
}
});
break;
case R.id.btnRidio:
mSingleChoiceID = -1;
builder.setTitle("單選框");
builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
mSingleChoiceID=which;
showDialogMSG("您選擇了ID是"+mSingleChoiceID+",名字為:"+items[which]);
}
});
builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
if(mSingleChoiceID>-1)
{
showDialogMSG("您最終選擇的是"+items[mSingleChoiceID]);
}else
{
showDialogMSG("您最終選擇的是"+items[0]);
}
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
break;
case R.id.btnCheck :
MultiChoiceID.clear(); //先清空集合
builder.setTitle("多項選擇");
builder.setMultiChoiceItems(items,new boolean[]{false,false,false,false,false,false}, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked)
{
MultiChoiceID.add(which);
showDialogMSG("您選擇了"+items[which]);
}else
{
MultiChoiceID.remove(which);
}
}
});
builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
String str="";
for(int i = 0;i<MultiChoiceID.size();i++)
{
str+=items[MultiChoiceID.get(i)]+",";
}
//去掉最後一個","
if(str.length()>0){
str = str.substring(0,str.length()-1);
}
showDialogMSG("你一共選擇的有"+str);
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
break;
case R.id.BtnProgressbar:
mProgressDialog =new ProgressDialog(MainActivity.this);
mProgressDialog.setTitle("進度條窗口");
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//設置樣式
mProgressDialog.setMax(MAX_PROGRESS);//設置最大值
mProgressDialog.setButton("確定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//這裡添加點擊後的邏輯
}
});
mProgressDialog.setButton2("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//這裡添加點擊後的邏輯
}
});
mProgressDialog.show();
new Thread().start();
return;
case R.id.btnCustom:
break;
case R.id.btnRead:
break;
}
builder.create().show();//創建並顯示彈出框
}
}
}
布局文件部分,比較簡單:
復制代碼 代碼如下:
<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/btnConfim"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="確定/取消"
/>
<Button
android:id="@+id/btnMore"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="多個消息提示"
/>
<Button
android:id="@+id/BtnList"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="列表框"
/>
<Button
android:id="@+id/BtnProgressbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="進度條框"
/>
<Button
android:id="@+id/btnRidio"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="單選框"
/>
<Button
android:id="@+id/btnCheck"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="多選框"
/>
<Button
android:id="@+id/btnCustom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="自定義布局"
/>
<Button
android:id="@+id/btnRead"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="讀取進度框"
/>
</LinearLayout>
前言以前也講過NDK開發,但是開始是抱著好玩的感覺去開始的,然後呢會helloWord就覺得大大的滿足,現在靜下來想這NDK開發到底是干什麼呢? NDK開發,其實是為了項
運行效果 產生原理自定義一組字符數組,隨機在裡面挑選出自己想要產生的驗證碼個數的字符,用畫筆畫入自己定義的BitMap中,然後在隨機畫入干擾線條,當然繪制時的一
在SystemUI中有一個Activity可以顯示所有的Logo這個Activity涉及到的圖標存放在SystemUI/res/drawable-nodpi目錄下在這裡我
新的Java8 API中提供了不少新的特性,其中就有Lambda表達式。而本文我們將主要介紹一下在Android開發過程中如何使用Lambda表達式,這裡主要是為我們後續