Android自訂對話方塊,android自訂
在android中有內建的對話方塊,為了美觀,很多開發人員會使用自訂對話方塊,如:
點擊“彈出自訂對話方塊按鈕後”顯示效果。
首先要自己定義一個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:background="@drawable/background" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:orientation="horizontal" > <ImageView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:src="@drawable/airsign" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="3" android:gravity="center" android:text="請輸入密碼" android:textColor="#FFFFFFFF" android:textSize="32px" android:textStyle="bold" /> </LinearLayout> <EditText android:id="@+id/et_pass" android:layout_width="match_parent" android:layout_height="40dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:background="@drawable/tv_background" android:paddingLeft="10dp"/> <LinearLayout android:layout_marginTop="10dp" android:layout_marginBottom="10dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/btn_ok" android:layout_width="wrap_content" android:layout_height="40dp" style="?android:attr/buttonStyleSmall" android:layout_weight="1" android:background="@drawable/background" android:textColor="#FFFFFFFF" android:text="確定"/> <Button android:id="@+id/btn_delete" android:layout_marginLeft="20dp" android:layout_width="wrap_content" android:layout_height="40dp" style="?android:attr/buttonStyleSmall" android:layout_weight="1" android:textColor="#FFFFFFFF" android:background="@drawable/background" android:text="取消"/> </LinearLayout></LinearLayout>
mainActivity中:
public class MainActivity extends Activity { private Context context = MainActivity.this; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void clickView(View v) { AlertDialog.Builder builder = new AlertDialog.Builder(this); // 建立對話方塊 AlertDialog dialog = builder.create(); // 沒有下面這句代碼會導致自訂對話方塊還存在原有的背景 builder.setView(View.inflate(this, R.layout.auto_dialog, null)); // 彈出對話方塊 dialog.show(); // 以下兩行代碼是對話方塊的EditText點擊後不能顯示IME的 dialog.getWindow().clearFlags( WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM); dialog.getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); // *** 主要就是在這裡實現這種效果的. // 設定視窗的內容頁面,shrew_exit_dialog.xml檔案中定義view內容 Window window = dialog.getWindow(); window.setContentView(R.layout.auto_dialog); EditText et_pass = (EditText) window.findViewById(R.id.et_pass); final Button btn_ok = (Button) window.findViewById(R.id.btn_ok); final Button btn_delete = (Button) window.findViewById(R.id.btn_delete); // 為兩個button按鈕添加點擊的監聽事件 btn_ok.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Toast.makeText(context, btn_ok.getText(), 0).show(); } }); btn_delete.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Toast.makeText(context, btn_delete.getText(), 0).show(); } }); }}