您好,登錄后才能下訂單哦!
Dialog中Android應用中的使用方法?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
Android開發中最常用的就是Dialog類,除了自定義dialog布局,最多的就是用在彈出對話框、進度條、輸入框、單選、復選框。
1、選擇對話框:
AlertDialog.Builder dialog = new AlertDialog.Builder(this); dialog.setTitle("選擇對話框"); dialog.setMessage("請選擇確認或取消"); dialog.setCancelable(false); //設置按下返回鍵不能消失 dialog.setPositiveButton("確認", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "您選擇了確認", Toast.LENGTH_LONG).show(); } }); dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "您選擇了取消", Toast.LENGTH_LONG).show(); } }); dialog.show();//顯示彈出窗口
2、進度條
ProgressDialog progressDialog = new ProgressDialog(this); progressDialog.setTitle("顯示進度條"); progressDialog.setMessage("加載中..."); progressDialog.setCancelable(true); progressDialog.show();
3、彈出輸入框
AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("彈出輸入框"); builder.setMessage("輸入內容"); final EditText editText = new EditText(this); builder.setView(editText); builder.setPositiveButton("確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "您輸入了" + editText.getText().toString(), Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("取消", null); builder.show();
4、彈出單選框
final AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("彈出單選框"); final String[] choice = new String[]{"Java", "C++", "PHP"}; builder.setSingleChoiceItems(choice, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "您選擇了" + choice[which], Toast.LENGTH_LONG).show(); dialog.dismiss(); // 讓窗口消失 } }); builder.create().show();
5、彈出復選框
final AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("彈出多選框"); final String[] choice = new String[]{"Android", "IOS", "Windos Phone"}; // 選擇項 final boolean[] isCheck = new boolean[choice.length]; // 是否被選中 builder.setMultiChoiceItems(choice, null, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { if (isChecked == true) isCheck[which] = true; else if(isChecked == false) isCheck[which] = false; } }); builder.setNegativeButton("取消", null); builder.setPositiveButton("確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String isSelected = "您選擇了"; for (int i = 0; i < choice.length; i++) { if (isCheck[i] == true) { isSelected += choice[i]+","; // 將選擇的內容加上 } } Toast.makeText(MainActivity.this, isSelected, Toast.LENGTH_LONG).show(); } }); builder.create(); builder.show();
6、自定義布局的Dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this); final AlertDialog dialog = builder.create(); View view = View.inflate(this, R.layout.personal_modifyhead_dialog, null); dialog.setView(view, 0, 0, 0, 0);// 設置邊距為0,保證在2.x的版本上運行沒問題 dialog.show(); AutoRelativeLayout rl_photograph = (AutoRelativeLayout) view.findViewById(R.id.rl_photograph); rl_photograph.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { presenter.modiftHeadFromPhotograph(); dialog.dismiss(); } }); AutoRelativeLayout rl_album = (AutoRelativeLayout) view.findViewById(R.id.rl_album); rl_album.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { presenter.modifyHeadFromAlbum(); dialog.dismiss(); } });
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。