俗話說 實踐出真知 所以 就直接上代碼啦
1院究、activity_main.xml
很基礎(chǔ) 沒什么好說的 順著寫就好
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
tools:ignore="HardcodedText">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World !"
android:textSize="30sp"
android:textColor="#159036" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="普通對話框"
android:textSize="25sp" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="單選對話框"
android:textSize="25sp"/>
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="多選對話框"
android:textSize="25sp"/>
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="進度條加載"
android:textSize="25sp"/>
<Button
android:id="@+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自定義彈窗"
android:textSize="25sp"/>
</LinearLayout>
主界面.jpg
2沟娱、MainActivity.java
這里展示了5種彈窗(對話框) 的用法 但是大同小異
PS: 剛開始準備寫在一起 但是太丑了 我都看不下去 還是分開展示吧 好氣哦
0)主體
public class MainActivity extends AppCompatActivity {
private final Context context = MainActivity.this;
private Button button1;
private Button button2;
private Button button3;
private Button button4;
private Button button5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
click1();
}
});
button2 = findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
click2();
}
});
button3 = findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
click3();
}
});
button4 = findViewById(R.id.button4);
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
click4();
}
});
button5 = findViewById(R.id.button5);
button5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
click5();
}
});
}
}
1)普通對話框
public void click1(){
//new一個建立彈窗模板的方法
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("基礎(chǔ)彈窗");
builder.setMessage("彈窗是否顯示成功?");
builder.setNegativeButton("否", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(context, "啊 這募闲。笔喉。尸诽。", Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(context, "彈窗顯示成功", Toast.LENGTH_SHORT).show();
}
});
builder.show();
}
普通對話框展示.jpg
2)單選對話框
public void click2(){
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("請選擇你最喜歡的選項");
final String items[] = {"A","B","C","D","E"};
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String item = items[which];
Toast.makeText(getApplicationContext(),"你選擇了:" + item.toString(),Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
builder.show();
}
單選對話框展示.jpg
3)多選對話框
public void click3(){
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("今天做了什么?");
final String items[] = {"吃飯","學習","睡覺","打豆豆"};
final boolean[] checkedItems = {false,true,false,false};
builder.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {}
});
builder.setPositiveButton("我選好了", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < checkedItems.length; i++) {
if(checkedItems[i]){
String option = items[i];
sb.append(option + " ");
}
}
Toast.makeText(getApplicationContext(),"今天去 " + sb.toString() + "了",Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
builder.show();
}
多選對話框.jpg
4)加載進度條
public void click4(){
final ProgressDialog dialog = new ProgressDialog(context);
dialog.setTitle("任務(wù)加載中...");
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
new Thread(){ //創(chuàng)建一個子線程
public void run(){
dialog.setMax(100);
for (int i = 0; i <= 100; i++) {
dialog.setProgress(i);
SystemClock.sleep(50);
}
dialog.dismiss();
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),"加載成功",Toast.LENGTH_SHORT).show();
//可以同時設(shè)置多個view 或者發(fā)生消息(只允許運行在主線程的操作)
//安卓中的主線程叫UI線程 一個activity只允許有一個
}
});
}
}.start();
dialog.show();
}
加載進度條展示.jpg
5)自定義彈窗
public void click5(){
//new一個彈窗 并綁定布局
View view1 = View.inflate(context,R.layout.layout_one,null);
AlertDialog dialog = new AlertDialog.Builder(context)
.setView(view1)
.create();//只有這個方法的返回類型是 AlertDialog
TextView textView = view1.findViewById(R.id.text1);
//textView.setText("haha world!");
dialog.setCanceledOnTouchOutside(false);//觸摸旁邊不會消失忽孽,但返回鍵會消失
//dialog.setCancelable(false);//都不會消失
dialog.show();
}
自定義彈窗展示.jpg
3绑改、layout_one.xml
這個就是寫自定義彈窗的布局
PS: 鑒于本人水平太菜了 有些布局就強行加空格寫的 哈哈哈哈偷懶了偷懶了
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
tools:ignore="HardcodedText">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" Hello world!"
android:textSize="30sp"
android:textColor="#159036"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" "/>
<Button
android:id="@+id/buttonX"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="嘻嘻"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" "/>
<Button
android:id="@+id/buttonH"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="哈哈"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
大概就這么多了吧 還是那句話 實踐出真知! and 筆記都在注釋里!