从零开始学android -- dialog
Posted 从Android出发
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从零开始学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); //去掉action return 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 -- notification通知
从零开始学android -- CilpDrawable 徐徐展开的风景
Android自定义View——从零开始实现书籍翻页效果(一)