国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android編程自定義Dialog的方法分析

2019-10-23 18:35:48
字體:
來源:轉載
供稿:網友

本文實例講述了Android編程自定義Dialog的方法。分享給大家供大家參考,具體如下:

功能:

android 提供給我們的只有2種Dialog 即 AlertDialog & ProgressDialog 但是 Dialog 有其自身的特點:1. 不是 Activity 2. 開銷比 Activity 小得多

鑒于以上的優點 我們就有定制自己Dialog 的需求

Android,自定義,Dialog

原理:

1. android 系統提供了一個class: Dialog. 而且你可以把自己的工作放在"protected void onCreate(Bundle savedInstanceState)" 在里面先調用系統的"super.onCreate(savedInstanceState)" 其就會保證調用這個method.

2. 至于 Dialog 界面的定制 可以寫一個xml 文件 然后 在 "void onCreate(Bundle)" 通過 "setContentView()" 來使之生效

3. Dialog 使用問題: 1. 彈出:show() 2. 取消:dismiss()

代碼:

1. 創建一個 Dialog:

public class CustomDialog extends Dialog {  public CustomDialog(Context context) {    super(context);    // TODO Auto-generated constructor stub  }  protected void onCreate(Bundle savedInstanceState){    super.onCreate(savedInstanceState);    setContentView(R.layout.custom_dialog);    setTitle("Custom Dialog");    TextView text = (TextView)findViewById(R.id.text);    text.setText("Hello, this is a custom dialog!");    ImageView image = (ImageView)findViewById(R.id.image);    image.setImageResource(R.drawable.sepurple);    Button buttonYes = (Button) findViewById(R.id.button_yes);    buttonYes.setHeight(5);    buttonYes.setOnClickListener(new Button.OnClickListener(){      public void onClick(View v) {        // TODO Auto-generated method stub        dismiss();      }    });    Button buttonNo = (Button) findViewById(R.id.button_no);    buttonNo.setSingleLine(true);    buttonNo.setOnClickListener(new Button.OnClickListener(){      public void onClick(View v) {        // TODO Auto-generated method stub        dismiss();      }    });  }  //called when this dialog is dismissed  protected void onStop() {    Log.d("TAG","+++++++++++++++++++++++++++");  }}

2. Dialog 的使用:

public class CustomDialogUsage extends Activity {  CustomDialog cd;  /** Called when the activity is first created. */  @Override  public void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.main);    cd = new CustomDialog(this);    Button buttonYes = (Button) findViewById(R.id.main_button);    buttonYes.setOnClickListener(new OnClickListener(){      public void onClick(View v) {        // TODO Auto-generated method stub        cd.show();      }    });  }}

3. Dialog 的界面定制:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/layout_root" android:orientation="horizontal"android:layout_width="fill_parent" android:layout_height="fill_parent"android:padding="10dp">  <ImageView android:id="@+id/image" android:layout_width="wrap_content"  android:layout_height="fill_parent" android:layout_marginRight="10dp"  />  <LinearLayout android:orientation="vertical"  android:layout_width="fill_parent" android:layout_height="wrap_content"  android:padding="5px">    <TextView android:id="@+id/text" android:layout_width="wrap_content"    android:layout_height="fill_parent"    android:textColor="#FFF" />    <LinearLayout android:orientation="horizontal"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:padding="5px">      <Button android:id="@+id/button_yes"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:text=" Yes " android:gravity="center"      />      <Button android:id="@+id/button_no"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:text=" No " android:gravity="center"      />    </LinearLayout>  </LinearLayout></LinearLayout>

希望本文所述對大家Android程序設計有所幫助。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 从化市| 平邑县| 镇雄县| 宜宾市| 石渠县| 澳门| 盐山县| 唐海县| 崇左市| 马关县| 井研县| 察隅县| 明星| 左云县| 安宁市| 新绛县| 南城县| 来宾市| 兴宁市| 太原市| 东明县| 德令哈市| 义乌市| 修文县| 楚雄市| 达孜县| 阳泉市| 庄浪县| 炉霍县| 高唐县| 东平县| 庆安县| 九台市| 景宁| 韶关市| 比如县| 资中县| 桂东县| 甘孜| 黄浦区| 怀化市|