雜亂之android的AlertDialog應用

來源:互聯網
上載者:User

這是代碼的,當然我在程式中提供了3個方法,調用不同的方法會彈出不同的alterDialog。

 

主要的實現是在activity當中。

package cn.com.chenzheng_java;</p><p>import android.app.Activity;<br />import android.app.AlertDialog;<br />import android.content.DialogInterface;<br />import android.content.res.Resources;<br />import android.graphics.drawable.Drawable;<br />import android.os.Bundle;<br />import android.view.LayoutInflater;<br />import android.view.View;<br />import android.widget.Button;<br />import android.widget.TextView;</p><p>/**<br /> *<br /> * @author chenzheng_java<br /> *<br /> */<br />public class AlertDialogActivity extends Activity {<br />private TextView textView;<br />@Override<br />public void onCreate(Bundle savedInstanceState) {<br />super.onCreate(savedInstanceState);<br />setContentView(R.layout.main);<br />textView = (TextView) findViewById(R.id.textView);<br />Button button = (Button) findViewById(R.id.button);<br />button.setOnClickListener(new View.OnClickListener() {</p><p>@Override<br />public void onClick(View v) {</p><p>showAlertDialog2();</p><p>}<br />});<br />}</p><p>/**<br /> * 彈出系統內建的alertDialog<br /> * setNegativeButton setPositiveButton setNeutralButton三種方法添加的按鈕的唯一不同就是表示符號不一樣<br /> * 這三種方法如果調用多次,並不會添加多個按鈕,而是,同類型的會進行覆蓋,後者覆蓋前者。<br /> */<br />private void clickSimpleAlertDialog() {<br />AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);<br />// 設定標題<br />alertDialog.setTitle("AlertDialog測試");<br />Resources r = this.getResources();<br />Drawable d = r.getDrawable(R.drawable.icon);<br />alertDialog.setIcon(d);<br />alertDialog.setMessage("花兒花兒滿天飛");<br />alertDialog.setPositiveButton("確定",<br />new DialogInterface.OnClickListener() {<br />@Override<br />public void onClick(DialogInterface dialog, int which) {<br />textView.setText("確定" + which);//whitch=-1<br />}<br />});</p><p>alertDialog.setNeutralButton("hello",<br />new DialogInterface.OnClickListener() {<br />@Override<br />public void onClick(DialogInterface dialog, int which) {<br />textView.setText("hello" + which);//whitch=-3<br />}<br />});<br />alertDialog.setNegativeButton("取消",<br />new DialogInterface.OnClickListener() {<br />@Override<br />public void onClick(DialogInterface dialog, int which) {<br />textView.setText("取消" + which);//whitch=-2<br />}<br />});<br />alertDialog.show();<br />}</p><p>/**<br /> * 載入自訂的布局格式到AlertDialog<br /> */<br />private void showCustomLayoutAlertDialog(){<br />/*<br /> * 根據使用者提供的布局檔案,擷取一個view<br /> * */<br />LayoutInflater flater = LayoutInflater.from(this);<br />View alertDialogView = flater.inflate(R.layout.alertdialog, null);</p><p>new AlertDialog.Builder(this).setTitle("title").setPositiveButton("確定",<br />new DialogInterface.OnClickListener() {<br />@Override<br />public void onClick(DialogInterface dialog, int which) {<br />textView.setText("確定" + which);//whitch=-1<br />}<br />}).setView(alertDialogView).show();</p><p>}</p><p>private void showAlertDialog2(){<br />AlertDialog.Builder dialog = new AlertDialog.Builder(this);<br />dialog.setTitle(R.string.hello);<br />String [] dataArray = new String[]{"hello","baby","word"};<br />dialog.setItems(dataArray, new DialogInterface.OnClickListener() {<br />/***<br /> * 我們這裡傳遞給dialog.setItems方法的參數為數組,這就導致了我們下面的<br /> * onclick方法中的which就跟數組下標是一樣的,點擊hello時返回0;點擊baby返回1……<br /> */<br />@Override<br />public void onClick(DialogInterface dialog, int which) {<br />dialog.dismiss();<br />textView.setText("點擊了按鈕:which="+which+" 按鈕:");</p><p>}<br />}).show();</p><p>}</p><p>}

 

在這裡,我們的alertDialog.xml代碼為<?xml version="1.0" encoding="utf-8"?><br /><LinearLayout<br /> xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:orientation="vertical"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"><br /> <EditText<br /> android:id="@+id/editText"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:minLines="1"<br /> /><br /> <EditText<br /> android:id="@+id/editText2"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:minLines="1"<br /> /><EditText<br /> android:id="@+id/editText3"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:minLines="1"<br /> /><br /> <Button<br /> android:id="@+id/button1"<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:text="hello"<br /> /></p><p></LinearLayout><br />

main.xml代碼

<?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:orientation="vertical"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"<br /> ><br /><Button<br />android:id="@+id/button"<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:text="show AlertDialog"<br /> /><br /> <TextView<br />android:id="@+id/textView"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:text=""<br /> /><br /></LinearLayout><br />

運行便可得到相應的結果。

----------------------------------------------------------------------------------

用法解析:

 

這裡執行個體化AlterDialog時一定要注意了,它本身是沒有可供直接調用的構造方法的,我們是通過new AlterDialog.Builder()來建立執行個體的。

AlterDialog上不光可以放置按鈕,還可以放置很多東西,在代碼中我們用到的就有,數組資訊列表、按鈕、圖片、EditText等等等等……

使用者要想知道我們點擊了alertDialog上的哪一個元素,是通過DialogInterface.OnClickListener的onclick方法裡的which參數來決定的,想要AlterDialog消失則調用dialog.dismiss()方法!

如果我們設定cancelable()為true的話,我們就可以通過返回鍵來關閉他。

setSingleChoiceItems,設定為單選項對話方塊

setMultiChoiceItems設定為多選選項對話款

setItems,設定為選項對話方塊,不區分多選單選

 

setCustomTitle(View customTitleView),設定自訂的Title視圖

setView(View view),設定對話方塊內容為自訂的視圖

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.