嘗試在您的代碼中進行此操作。
public void showAlertDialogButtonClicked(View view) {
// create an alert builder
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Name");
// set the custom layout
final View customLayout = getLayoutInflater().inflate(R.layout.custom_layout, null);
builder.setView(customLayout);
// add a button
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// send data from the AlertDialog to the Activity
EditText editText = customLayout.findViewById(R.id.editText);
sendDialogDataToActivity(editText.getText().toString());
}
});
// create and show the alert dialog
AlertDialog dialog = builder.create();
dialog.show();
}
使用來自
<Button android:layout_width="match_parent"
android:layout_height="wrap_content" android:onClick="showAlertDialogButtonClicked"/>
我發現我在做什麼錯了這種方法,我需要先找到充氣視圖的ImageButton。 – Yvonne 2011-04-28 02:48:42