編輯:關於Android編程
一、基本知識點
常見的dialog
基本代碼:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
AlertDialog dialog = builder.create();
dialog.show();
1)常見對話框
builder.setMessage("浏覽傳智播客的網站");
builder.setPositiveButton
2)選擇對話框
builder.setItems(items, new DialogInterface.OnClickListener()
<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD48cD5pdGVtc8rH0ru49tGhz+7E2sjdtcTK/dfpPGJyIC8+PGJyIC8+PGltZyBzcmM9"/uploadfile/Collfiles/20140822/201408220838092.jpg" alt="\" />
3)單選對話框
builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener()
第二個參數:默認選中的按鈕
4)多選對話框
builder.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener()
5)自定義對話框
LayoutInflater mInflater = LayoutInflater.from(this);
View view = mInflater.inflate(R.layout.custom_dialog, null);
builder.setView(view);
二、示例代碼
1、MainActivity
package com.example.dialogtest; import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.content.DialogInterface.OnMultiChoiceClickListener; import android.view.LayoutInflater; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void commondialog(View view){ AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setCancelable(false); builder.setTitle("女神"); builder.setMessage("章澤天是你的女神嗎?"); builder.setPositiveButton("確定", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); builder.setNeutralButton("隱藏", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }); builder.setNegativeButton("取消", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); AlertDialog dialog = builder.create(); dialog.show(); } public void selectdialog(View view){ AlertDialog.Builder builder = new AlertDialog.Builder(this); final String items[] = new String[]{ "章澤天", "章澤天1", "章澤天2", "章澤天3" }; builder.setTitle("選擇女神"); builder.setItems(items, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(), items[which], 0).show(); } }); AlertDialog dialog = builder.create(); dialog.show(); } public void singleselectdialog(View view){ AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("選擇女神"); final String items[] = new String[]{ "章澤天", "章澤天1", "章澤天2", "章澤天3" }; builder.setSingleChoiceItems(items, 0, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(), items[which], 0).show(); } }); builder.setPositiveButton("確定", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); AlertDialog dialog = builder.create(); dialog.show(); } public void multiselectdialog(View view){ AlertDialog.Builder builder = new AlertDialog.Builder(this); final String items[] = new String[]{ "章澤天", "章澤天1", "章澤天2", "章澤天3", }; final boolean checkedItems[] = new boolean[]{ true,false,false,false }; builder.setTitle("選擇女神"); builder.setMultiChoiceItems(items, checkedItems, new OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { Toast.makeText(getApplicationContext(), items[which], 0).show(); } }); builder.setPositiveButton("確定", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); AlertDialog dialog = builder.create(); dialog.show(); } public void customdialog(View view){ LayoutInflater inflater = LayoutInflater.from(this); View mView = inflater.inflate(R.layout.custom_dialog, null); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("請輸入用戶信息"); builder.setView(mView); final AlertDialog dialog = builder.create(); dialog.show(); Button okBtn = (Button) mView.findViewById(R.id.bt_ok); okBtn.setOnClickListener(new View.OnClickListener() {//注意,這個demo用到了兩個OnClickListener.所以這裡需要加上View @Override public void onClick(View v) { dialog.dismiss(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
三、源碼下載
http://download.csdn.net/detail/caihongshijie6/7799073
四、效果圖
首先從字面意思理解兩個詞onTouchEvent:觸發觸摸事件onInterceptTouchEvent:觸發攔截觸摸事件通過查看源代碼及類繼承關系onIntercept
背景最近的項目中用到了類似美團中的下拉多選菜單,在實際開發過程中,也發現了一些問題,主要歸納如下:1.當菜單較為復雜時,如果不能設計好代碼邏輯,將造成控件難於維護2.美團
什麼是通信?通信 ,顧名思義,指的就是信息的傳遞或者交換看完本文能收獲什麼?按目錄索引,你可以學習到1. 組件間的通信,Activity,fragment,Service
手機QQ同步助手可以備份短信和聯系人到網絡中,你只要下載並安裝QQ同步助手並用QQ號登錄,就可以將你的短信和聯系人備份到網絡中,當你換手機或者格機的時候就可