根据需求写了一个自定义的引用布局Dialog。
1、xml 布局代码。
<?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="@drawable/bg_main_menu"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:layout_marginLeft="30dip"
android:layout_marginRight="30dip"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/str_chnametitle"
android:textSize="18sp" >
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="49dp"
android:layout_marginBottom="10dip"
android:layout_marginLeft="30dip"
android:layout_marginRight="30dip"
android:layout_marginTop="15dip"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/str_chname"
android:textSize="15sp" >
</TextView>
<EditText
android:id="@+id/edit_channel"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:gravity="center"
android:layout_marginLeft="10dp"
android:textColor="#FFFFFF"
android:singleLine="true"
android:focusable="true"
android:hint="@string/str_input"
android:focusableInTouchMode="true"
android:maxLength="4"
android:inputType="number"
android:background="@drawable/edit_bg"
android:textSize="13sp" >
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="40dp"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="20dp">
<Button
android:id="@+id/btn_yes"
android:layout_width="100dp"
android:layout_height="40dp"
android:gravity="center"
android:textColor="#FFFFFF"
android:text="@string/str_yes"
android:background="@drawable/reset_background_selector"
/>
<Button
android:id="@+id/btn_canecl"
android:layout_width="100dp"
android:layout_height="40dp"
android:gravity="center"
android:layout_marginLeft="13dp"
android:textColor="#FFFFFF"
android:text="@string/str_cancel"
android:background="@drawable/reset_background_selector"
/>
</LinearLayout>
</LinearLayout>
2、java代码引用
public void showDialog(){
LayoutInflater inflater = LayoutInflater.from(this);
View layout=inflater.inflate(R.layout.edit_channel, null);
final AlertDialog.Builder builder =new AlertDialog.Builder(this);
builder.setView(layout);
builder.setCancelable(false);
final AlertDialog dialog = builder.show();
isShwoDialog = true;
WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
params.width = 600;
params.height = 400;
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.getWindow().setAttributes(params);
final EditText chEditText=(EditText)layout.findViewById(R.id.edit_channel);
chEditText.setFocusableInTouchMode(true);
Button btYesButton=(Button)layout.findViewById(R.id.btn_yes);
Button btCaButton=(Button)layout.findViewById(R.id.btn_canecl);
btYesButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String strnum= chEditText.getText().toString();
if (strnum.equals("")) {
Toast.makeText(TSBHotelMode.this, R.string.str_isey, Toast.LENGTH_SHORT).show();
}else {
int chNum=Integer.valueOf(strnum).intValue();
SharedPreferences sharedPreferences = getSharedPreferences("count_valuse", Context.MODE_WORLD_READABLE);
Editor editor = sharedPreferences.edit();
editor.putInt("num", chNum);
editor.commit();
mHandler.sendEmptyMessage(MESSGER_CHAN);
dialog.dismiss();
}
}
});
btCaButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//--dismiss()---
dialog.dismiss();
}
});
}
3、调用
在需要显示它的地方
showDialog();