編輯:關於Android編程
最近在做項目,小組幾個回了家。界面暫時沒人做,用到自定義對話框只能臨時去學。現在把對話框的相關整理。
package com.example.alertdialog; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.view.View.OnClickListener; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { private Button button1; private Button button2; private final CharSequence[] items = {"廣州","深圳","上海","北京"}; private Button button3; private Button button4; private Button button5; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); button1 = (Button) findViewById(R.id.button1); button2 = (Button) findViewById(R.id.button2); button3 = (Button) findViewById(R.id.button3); button4 = (Button) findViewById(R.id.button4); button5 = (Button) findViewById(R.id.button5); button1.setOnClickListener(new OnClickListener() { public void onClick(View view) { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("提示"); builder.setMessage("你確定要刪除嗎!"); builder.setIcon(R.drawable.ic_launcher); builder.setPositiveButton("確定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int arg1) { // 讓對話框消失 dialog.dismiss(); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface arg0, int arg1) { // TODO Auto-generated method stub } }); builder.setNeutralButton("忽略", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { // TODO Auto-generated method stub //忽略用戶操作,作用和取消一樣 } }); AlertDialog alertDialog = builder.create(); alertDialog.show(); } }); button2.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("提示"); //不能設置setMessage的內容,不然會覆蓋原來item的值 //在OnClickListener前要加DialogInterface,不然會報錯 builder.setItems(items, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int index) { //item[index]不能轉化為String,轉化為CharSequence就行了 CharSequence select_item = items[index]; Toast.makeText(MainActivity.this, "你選擇了城市"+select_item, 1).show(); } }); AlertDialog alertDialog = builder.create(); alertDialog.show(); } }); button3.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("請選擇以下城市"); builder.setIcon(R.drawable.ic_launcher); /* * 第一個參數:顯示在對話框中供選擇列表 * 第二個參數:默認選擇的值,-1表示一個都沒選 * 第三個參數:監聽器 */ builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub CharSequence select_item = items[which]; Toast.makeText(MainActivity.this, "-->>"+select_item, 1).show(); dialog.dismiss(); } }); AlertDialog alertDialog = builder.create(); alertDialog.show(); } }); button4.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("請選擇以下城市"); /* * 第一個參數:供選擇多選列表 * 第二個參數:boolean類型的數組,表示選中的項;如設置為null,表示沒有選項默認被選中 * 第三個參數:不用說,監聽器 */ builder.setMultiChoiceItems(items, new boolean[]{false,false,true,false}, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface arg0, int arg1, boolean ischecked) { // TODO Auto-generated method stub } }); builder.setPositiveButton("確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { // 直接獲取選擇的值 } }); AlertDialog alertDialog = builder.create(); alertDialog.show(); } }); //其實是button4的另一種寫法 button5.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("請選擇以下城市"); //獲取選擇值:使用StringBuffer追加,中間用逗號分隔開來 builder.setMultiChoiceItems(R.array.city, new boolean[]{false,false,true,false}, new DialogInterface.OnMultiChoiceClickListener() { public void onClick(DialogInterface arg0, int arg1, boolean arg2) { // TODO Auto-generated method stub } }); builder.setPositiveButton("確定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface arg0, int arg1) { // 獲取選擇的值 } }); AlertDialog alertDialog = builder.create(); alertDialog.show(); } }); } }
Win10系統下配JDK環境變量問題電腦系統win10jdk版本jdk1.8.0_102Jdk安裝在了c盤下 配的環境變量為JAVA_HOME CLA
ListView是安卓中非常常用的一個控件。安卓設計使用Adapter來對ListView進行管理。但是系統提供的Adapter無法滿足一些復雜的顯示情況,這個時候我們就
DrawerLayout顧名思義就是一個管理布局的。使用方式可以與其它的布局類類似。DrawerLayout帶有滑動的功能。只要按照drawerLayout的規定布局方式
今天接到一個任務,需要解決同事在美國測試Voicemail功能時,出現的下載失敗問題。目前,國內的運營商似乎沒有支持Voicemail功能,因此資料相對較少。自己以前對這