標籤:
android自訂對話方塊
2015-04-02 18:27:02
有的時候安卓內建的對話方塊不能滿足我們的需要,或許是功能不齊全亦或者不符合我們的介面設計要求,這個時候我們就需要弄一個我們自己設定的對話方塊了,下面我將為大家講一種很簡單的方法建立自己的對話方塊。
首先建立一個style 即:<style name="你自己命名" parent="@android:Theme.Dialog">.......</style>這個相信大家都懂的。
接著建立你自己的對話方塊布局:自己要怎樣的自己設計
建立一個自己的Dialog類繼承Dialog,即:
public Mydialog extends Dialog{
這個類裡要包含這個構造方法
public Mydialog(Context context, int theme) {
super(context, theme);
this.context = context;
}
接著建立這個方法,其實跟Activity差不多的
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.你建立的dialog布局);
}
}
上面要注意的是:如果你對話方塊有要處理的事件,比如退出對話方塊或者其他點擊按鈕的最好實現OnClickListener介面
接著就是在Activity中使用你自己的Dialog了
Dialog dialog = new Mydialog(ApplicationCarActivity.this,
R.style.MyDialog);注意R.style.MyDialog這個就是開始你建立的那個style
下面我把我做的過程的代碼放在下面:
首先建立的style:
<style name="MyDialog" parent="@android:Theme.Dialog"> <item name="android:windowFrame">@null</item> <item name="android:windowNoTitle">true</item> <item name="android:windowBackground">@drawable/ic_launcher</item> <item name="android:windowIsFloating">true</item> <item name="android:windowContentOverlay">@null</item> </style>
裡面的內容我就不解釋了
接著是我的對話方塊布局:布局隨便搞的,大家看著就好
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <DatePicker android:id="@+id/date_picker1" android:calendarViewShown="false" android:layout_width="wrap_content" android:layout_height="150dp" style="@style/AppTheme"/> <TimePicker android:id="@+id/time_picker1" android:layout_width="wrap_content" android:layout_height="150dp"/> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center"> <Button android:id="@+id/butn_sure" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="確定"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#ffffff" android:text=" "/> <Button android:id="@+id/butn_no" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="取消"/> </LinearLayout></LinearLayout>
這個是自訂的Dialog
class Mydialog extends Dialog implements android.view.View.OnClickListener { private Context context; private Button butn; private TimePicker timePicker; private DatePicker datePicker; // public Mydialog(Context context) { // super(context); // // // TODO Auto-generated constructor stub // } public Mydialog(Context context, int theme) { super(context, theme); this.context = context; // TODO Auto-generated constructor stub } protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); // setTheme(android.R.style.Theme_DeviceDefault_Light_NoActionBar_Fullscreen); this.setContentView(R.layout.dialog_test); datePicker = (DatePicker) findViewById(R.id.date_picker1); timePicker = (TimePicker) findViewById(R.id.time_picker1); //resizePikcer(datePicker); butn = (Button) findViewById(R.id.butn_sure); // edt = (EditText) findViewById(R.id.number); butn.setOnClickListener(this); } @Override public void onClick(View arg0) { // TODO Auto-generated method stub switch (arg0.getId()) { case R.id.butn_sure: // String str = edt.getText().toString(); // edt1.setText(str); StringBuffer sb = new StringBuffer(); sb.append(String.format("%d-%02d-%02d %02d:%02d", datePicker.getYear(), datePicker.getMonth() + 1, datePicker.getDayOfMonth(), timePicker.getCurrentHour(), timePicker.getCurrentMinute())); if (i == 1) { edt5.setText(sb); } else if (i == 2) { edt6.setText(sb); } this.dismiss(); break; } } }
最後是在Activity的使用,我寫在一個方法裡:
private void mydialog() { Dialog dialog = new Mydialog(ApplicationCarActivity.this, R.style.MyDialog); Window window = dialog.getWindow();// builder.getWindow(); window.setGravity(Gravity.BOTTOM); // 此處可以設定dialog顯示的位置 window.setWindowAnimations(R.style.mystyle); //dialog.setTitle("請設定時間"); dialog.show(); //dialog.getWindow().setLayout(620, 414); /* * 將對話方塊的大小按螢幕大小的百分比設定 */ WindowManager m = getWindowManager(); Display d = m.getDefaultDisplay(); // 擷取螢幕寬、高用 WindowManager.LayoutParams p = window.getAttributes(); // 擷取對話方塊當前的參數值 p.height = (int) (d.getHeight()*0.4); // 高度設定為螢幕的0.6 p.width =(int) (d.getWidth()); // 寬度設定為螢幕的0.65 window.setAttributes(p); }
大家互相參考學習,小作獻醜了。謝謝大家光臨寒舍!
android自訂對話方塊