我希望這對你有幫助。我實現這種方式的自定義對話框
public void showUpdateDialog() {
updateDialog = new Dialog(this,
android.R.style.Theme_Translucent_NoTitleBar_Fullscreen) {
@Override
public void onBackPressed() {
this.dismiss();
loadDataAsync.execute();
}
};
updateDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
updateDialog.getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
updateDialog.setContentView(R.layout.updatepopup);
logger.info("Network error popup on");
updateDialog.setCancelable(true);
ImageView dialogImage = (ImageView) updateDialog
.findViewById(R.id.dialogheaderimageupdateupdate);
dialogImage.setBackgroundResource(R.drawable.infoimage);
TextView dialogMesage = (TextView) updateDialog
.findViewById(R.id.dialogmessgetextupdate);
TextView currVersion = (TextView) updateDialog
.findViewById(R.id.currentversionupdate);
TextView newVersion = (TextView) updateDialog
.findViewById(R.id.newversionupdate);
TextView dialogHeader = (TextView) updateDialog
.findViewById(R.id.dialogheadertextupdate);
newVersion.setText("Latest version: " + versionCode);
currVersion.setText("Current version : " + currentVersionName);
dialogMesage.setText("Would you like to update now?");
dialogHeader.setText("A program update is available! ");
Button dialogOk = (Button) updateDialog
.findViewById(R.id.dialogokbuttonupdate);
dialogOk.setText("Update");
dialogOk.setFocusable(true);
dialogOk.requestFocus();
dialogOk.setFocusableInTouchMode(true);
dialogOk.requestFocus();
Button dialogCancel = (Button) updateDialog
.findViewById(R.id.dialogcancelbuttonupdate);
dialogOk.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
updateDialog.dismiss();
downLoad = new DownLoad();
downLoad.execute(apkUrl.trim());
}
});
dialogCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
updateDialog.dismiss();
loadDataAsync.execute();
}
});
try {
updateDialog.show();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
而我updatepopup.xml下方顯示
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/layoutsample"
style="@android:style/Theme.Black.NoTitleBar.Fullscreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/dialoghdrbg"
android:orientation="horizontal" >
<ImageView
android:id="@+id/dialogheaderimageupdateupdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/dialogheadertextupdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|right"
android:layout_marginLeft="1dp"
android:gravity="center_vertical|right"
android:textColor="#000000"
android:textSize="25dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/dialogcontentbg"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical" >
<TextView
android:id="@+id/currentversionupdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#ffffff"
android:textSize="23dp"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/newversionupdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#ffffff"
android:textSize="23dp" />
<TextView
android:id="@+id/dialogmessgetextupdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#ffffff"
android:textSize="23dp"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|center_horizontal"
android:layout_marginTop="15dp"
android:gravity="center|center_horizontal"
android:orientation="horizontal" android:layout_marginBottom="10dp">
<Button
android:id="@+id/dialogokbuttonupdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:background="@drawable/buttonanimation"
android:text="@string/OKButton"
android:textSize="20dp"
android:textStyle="bold" />
<Button
android:id="@+id/dialogcancelbuttonupdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:background="@drawable/buttonanimation"
android:text="@string/CancelButton"
android:textSize="20dp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
嘗試增加'View',然後調用方法就像這些'discardButton.setOnClickListener(new View.onCl ickListener()...' –
試過了,沒有工作。它已經是一個View.OnClickListener。 – user1810737
有一個問題,你說這個代碼是在一個適配器中。你能確切地知道適配器的位置嗎?你可以多顯示一下你的代碼嗎? – Rajeev