知识的价值不在于占有,而在于使用。这篇文章主要讲述从零开始学android -- dialog相关的知识,希望能为你提供帮助。
先看个效果图
文章图片
activity_main.xml
< ?xml version="1.0" encoding="utf-8"?> < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> < Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="复选对话框" /> < Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="加载对话框" /> < Button android:id="@+id/button3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="显示进度的加载对话框" /> < Button android:id="@+id/button4" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="推荐创建对话框的方式创建对话框"/> < /LinearLayout>
DialogActivity.class
package zou.study.com.myfirstapp; import android.app.Dialog; import android.app.ProgressDialog; import android.content.DialogInterface; import android.os.Bundle; import android.os.SystemClock; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Toast; import zou.study.com.fragment.EditNameDialogFragment; public class DialogActivity extends AppCompatActivity implements View.OnClickListener{String[] items = {"Google","Apple","Microsoft"}; boolean[] itemChecked = new boolean[items.length]; ProgressDialog progressDialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); }private void initView() { findViewById(R.id.button1).setOnClickListener(this); findViewById(R.id.button2).setOnClickListener(this); findViewById(R.id.button3).setOnClickListener(this); findViewById(R.id.button4).setOnClickListener(this); }@Override public void onClick(View v) {switch (v.getId()){ case R.id.button1://普通的复选对话框 showDialog(1); break; case R.id.button2://进度对话框 final ProgressDialog dialog = ProgressDialog.show(this,"Do something","Please wait..."); new Thread(new Runnable() { @Override public void run() { SystemClock.sleep(5000); dialog.dismiss(); } }).start(); break; case R.id.button3://带进度调的对话框 showDialog(2); progressDialog.setProgress(0); new Thread(new Runnable() { @Override public void run() { for (int i = 0; i < = 15; i++){ SystemClock.sleep(1000); progressDialog.incrementProgressBy(100/15); } progressDialog.dismiss(); } }).start(); break; case R.id.button4: //采用android 3.0后推荐的创建dialog方式 DialogFragment 这里只是简单用法,想要了解更多请自己google EditNameDialogFragment editNameDialog = new EditNameDialogFragment(); editNameDialog.show(getFragmentManager(), "EditNameDialog"); break; } }@Override protected Dialog onCreateDialog(int id) { switch (id){ case 1: AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setIcon(R.mipmap.ic_launcher); builder.setTitle("标题"); builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getBaseContext(),"OK clicked",Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getBaseContext(),"cancel clicked",Toast.LENGTH_SHORT).show(); } }); builder.setMultiChoiceItems(items, itemChecked, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { Toast.makeText(getBaseContext(),items[which] + (isChecked?"checked!":"unchecked!"),Toast.LENGTH_SHORT).show(); } }); return builder.create(); case 2: progressDialog = new ProgressDialog(this); progressDialog.setIcon(R.mipmap.ic_launcher); progressDialog.setTitle("Downloading files..."); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getBaseContext(),"OK clicked",Toast.LENGTH_SHORT).show(); } }); progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getBaseContext(),"cancel clicked",Toast.LENGTH_SHORT).show(); } }); return progressDialog; } return null; } }
EditNameDialogFragment
package zou.study.com.fragment; import android.app.DialogFragment; import android.os.Bundle; import android.support.annotation.Nullable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.Window; import zou.study.com.myfirstapp.R; public class EditNameDialogFragment extends DialogFragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE); //去掉actionreturn inflater.inflate(R.layout.fragment_dialog,container); } }
fragment_dialog.xml
< ?xml version="1.0" encoding="utf-8"?> < RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" > < TextView android:id="@+id/id_label_your_name" android:layout_width="wrap_content" android:layout_height="32dp" android:gravity="center_vertical" android:text="Your name:" /> < EditText android:id="@+id/id_txt_your_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_toRightOf="@id/id_label_your_name" android:imeOptions="actionDone" android:inputType="text" /> < Button android:id="@+id/id_sure_edit_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_below="@id/id_txt_your_name" android:text="ok" /> < /RelativeLayout>
学习记录之用,如有错误请指正谢谢.
【从零开始学android -- dialog】
推荐阅读
- 搭建Android 开发环境
- [React] Configure a React & Redux Application For Production Deployment and Deploy to Now
- android ——可折叠式标题栏
- sharepoint 2013 使用Office Web Apps 查看或编辑office文档时页面出现(“抱歉,出现了问题”错误)
- tomcat启动停止在 Initializing Spring root WebApplicationContext,就不运行了
- Android开发笔记: 5种对话框案例
- Android自定义View——实现水波纹效果类似剩余流量球
- Android中监控home键
- Log4j自定义Appender