從零開始學android -- dialog

來源:互聯網
上載者:User

標籤:smis   mis   build   int   textview   input   media   created   thread   

先看個

 

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.