日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android经常使用的五种弹出对话框

發布時間:2024/4/14 Android 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android经常使用的五种弹出对话框 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

? 一個Android開發中經常使用對話框的小樣例,共同擁有五種對話框:普通彈出對話框,單選對話框,多選對話框,輸入對話框及進度條樣式對話框:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? xmlns:tools="http://schemas.android.com/tools"
? ? android:layout_width="match_parent"
? ? android:layout_height="match_parent"
? ? android:orientation="vertical" >


? ? <Button
? ? ? ? android:id="@+id/common_dialog"
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="40dp"
? ? ? ? android:text="普通對話框"
? ? ? ? android:textSize="16sp"
? ? ? ? android:layout_marginTop="10dp" />


? ? <Button
? ? ? ? android:id="@+id/radio_dialog"
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="40dp"
? ? ? ? android:text="單選對話框"
? ? ? ? android:textSize="16sp"
? ? ? ? android:layout_marginTop="10dp" ?/>


? ? <Button
? ? ? ? android:id="@+id/check_dialog"
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="40dp"
? ? ? ? android:text="多選對話框"?
? ? ? ? android:textSize="16sp"
? ? ? ? android:layout_marginTop="10dp" />


? ? <Button
? ? ? ? android:id="@+id/input_dialog"
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="40dp"
? ? ? ? android:text="輸入文字對話框"?
? ? ? ? android:textSize="16sp"
? ? ? ? android:layout_marginTop="10dp" />


? ? <Button
? ? ? ? android:id="@+id/progress_dialog"
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="40dp"
? ? ? ? android:text="進度條對話框"?
? ? ? ? android:textSize="16sp"
? ? ? ? android:layout_marginTop="10dp" />


</LinearLayout>

以下是輸入內容的簡單布局activity_input.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? xmlns:tools="http://schemas.android.com/tools"
? ? android:id="@+id/LinearLayout1"
? ? android:layout_width="match_parent"
? ? android:layout_height="match_parent"
? ? android:orientation="vertical" >


? ? <TextView
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:text="@string/hello_world" />


? ? <EditText
? ? ? ? android:id="@+id/uname"
? ? ? ? android:layout_width="fill_parent"
? ? ? ? android:layout_height="wrap_content" />


? ? <TextView
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:text="@string/hello_world" />


? ? <EditText
? ? ? ? android:id="@+id/upass"
? ? ? ? android:layout_width="fill_parent"
? ? ? ? android:layout_height="wrap_content" />


</LinearLayout>

代碼及凝視:

public class MainActivity extends Activity implements OnClickListener {
/**單選框模擬標題 大學*/
private final static int CHECKED_ENU = 0;
/**單選框模擬標題 ?高中*/
private final static int CHECKED_SEL = 1;
/**單選框模擬標題 ?初中*/
private final static int CHECKED_CHU = 2;
/**復選button狀態為全選 */
private boolean[] checked = { true, true, true, false };
/**模擬的進度值 */
private int progressNumber;
/**進度對話框 */
private ProgressDialog progressDialog;
/**相應button*/
private Button commonBtn, radioBtn, checkBtn, inputBtn, progressBtn;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
initListeners();
}


/**初始化UI控件*/


private void initViews() {
this.commonBtn = (Button) findViewById(R.id.common_dialog);
this.radioBtn = (Button) findViewById(R.id.radio_dialog);
this.checkBtn = (Button) findViewById(R.id.check_dialog);
this.inputBtn = (Button) findViewById(R.id.input_dialog);
this.progressBtn = (Button) findViewById(R.id.progress_dialog);
}


/**注冊button監聽事件*/
private void initListeners() {
this.commonBtn.setOnClickListener(this);
this.radioBtn.setOnClickListener(this);
this.checkBtn.setOnClickListener(this);
this.inputBtn.setOnClickListener(this);
this.progressBtn.setOnClickListener(this);
}


/**普通對話框 */
private Dialog buildAlertDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("對話框");
builder.setMessage("您的password不正確!!");


ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.mm1);
/**設置背景圖片*/
builder.setView(imageView);
/**左邊button*/
builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setTitle("您點擊的是左邊確定button!");
}
});
/**中間button*/
builder.setNeutralButton("詳情", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setTitle("您點擊的是中間詳情button!");
}
});
/**右邊button*/
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
setTitle("您點擊的是右邊取消button!");
}
});
return builder.create();
}


/**單選button彈出框 */
private Dialog buildAlertDialog_radio() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("對話框");
/**單選button,默認高中被選中*/
builder.setSingleChoiceItems(new String[] { "大學", "高中", "初中", "小學" }, 1, new DialogInterface.OnClickListener() {


@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
switch (which) {
case CHECKED_ENU:
setTitle("大學");
break;
case CHECKED_SEL:
setTitle("高中");
break;
case CHECKED_CHU:
setTitle("初中");
break;
default:
setTitle("小學");
break;
}
}
});


builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setTitle("您點擊的是左邊確定button!");
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setTitle("您點擊的是右邊取消button!");
}
});
return builder.create();
}


/**能夠多選button彈出框 */
private Dialog buildAlertDialog_checkbox() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("對話框");
/**復選button*/
builder.setMultiChoiceItems(new String[] { "大學", "高中", "初中", "小學" }, checked, new DialogInterface.OnMultiChoiceClickListener() {


@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
setTitle("which=" + which + "-----" + "isChecked=" + isChecked);
}
});


builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setTitle("您點擊了確定button!");
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
setTitle("您點擊的是了取消button!");
}
});
return builder.create();
}


/**含能夠輸入文本的彈出框 */
private Dialog buildAlertDialog_input() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("對話框");
LayoutInflater inflater = LayoutInflater.from(this);
builder.setView(inflater.inflate(R.layout.activity_input, null));
builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setTitle("您點擊的是確定button!");
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setTitle("您點擊的是取消button!");
}
});
return builder.create();
}


/**進度對話框 */
private Dialog buildAlertDialog_progress() {
progressDialog = new ProgressDialog(this);
progressDialog.setTitle("進度條");
progressDialog.setMessage("正在下載...........");
/**進度條樣式 */
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
/**模糊效果 */
progressDialog.setIndeterminate(false);
return progressDialog;
}


/**每隔0.3秒更新一次進度 */
public void updateProgress() {
new Thread() {
@Override
public void run() {
try {
while (progressNumber <= 100) {
progressDialog.setProgress(progressNumber++);
Thread.sleep(300);
super.run();
}
/**下載完后,關閉下載框 */
progressDialog.cancel();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
}


@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.common_dialog:
buildAlertDialog().show();
break;
case R.id.radio_dialog:
buildAlertDialog_radio().show();
break;
case R.id.check_dialog:
buildAlertDialog_checkbox().show();
break;
case R.id.input_dialog:
buildAlertDialog_input().show();
break;
case R.id.progress_dialog:
buildAlertDialog_progress().show();
updateProgress();
break;
default:
break;
}
}
}

總結

以上是生活随笔為你收集整理的Android经常使用的五种弹出对话框的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。