下面列出了android.app.Dialog#setContentView ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void optionLegend() {
// Show help
Dialog dialog = new Dialog(ActivityApp.this);
dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);
dialog.setTitle(R.string.menu_legend);
dialog.setContentView(R.layout.legend);
dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, getThemed(R.attr.icon_launcher));
((ImageView) dialog.findViewById(R.id.imgHelpHalf)).setImageBitmap(getHalfCheckBox());
((ImageView) dialog.findViewById(R.id.imgHelpOnDemand)).setImageBitmap(getOnDemandCheckBox());
for (View child : Util.getViewsByTag((ViewGroup) dialog.findViewById(android.R.id.content), "main"))
child.setVisibility(View.GONE);
((LinearLayout) dialog.findViewById(R.id.llUnsafe)).setVisibility(PrivacyManager.cVersion3 ? View.VISIBLE
: View.GONE);
dialog.setCancelable(true);
dialog.show();
}
@Override
public void showProgressDialog() {
dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
View dialogView = View.inflate(this, R.layout.inflater_waiting_for_connection, null);
mDummyView = dialogView.findViewById(R.id.dummy);
mExitButton = dialogView.findViewById(R.id.progress_exit);
mProgressText = (CTextView) dialogView.findViewById(R.id.progress_message);
mExitButton.setOnClickListener(this);
dialog.setCancelable(false);
dialog.setContentView(dialogView);
dialog.setCanceledOnTouchOutside(true);
dialog.show();
}
public static Dialog createLongPressMessageDialog(Context context, String title, boolean hide,
View.OnClickListener listener) {
Dialog dialog = new Dialog(context, IdHelper.getStyle(context, "jmui_default_dialog_style"));
View view = LayoutInflater.from(context).inflate(IdHelper.getLayout(context, "jmui_dialog_msg_alert"), null);
dialog.setContentView(view);
Button copyBtn = (Button) view.findViewById(IdHelper.getViewID(context, "jmui_copy_msg_btn"));
Button deleteBtn = (Button) view.findViewById(IdHelper.getViewID(context, "jmui_delete_msg_btn"));
if (hide) {
copyBtn.setVisibility(View.GONE);
}
copyBtn.setOnClickListener(listener);
deleteBtn.setOnClickListener(listener);
dialog.setCancelable(true);
dialog.setCanceledOnTouchOutside(true);
return dialog;
}
public void initHelpDialog() {
helpDialog = new Dialog(this);
helpDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
helpDialog.setContentView(R.layout.dialog_help_demo_item);
((TextView) helpDialog.findViewById(R.id.dialog_help_version_text)).setText(getString(R.string.version_text,
BuildConfig.VERSION_NAME));
View okButton = helpDialog.findViewById(R.id.help_ok_button);
TextView textView = helpDialog.findViewById(R.id.help_text_playstore);
textView.setMovementMethod(LinkMovementMethod.getInstance());
okButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
helpDialog.dismiss();
}
});
}
/**
* 删除对话框.
*
* @param ctx the ctx
* @param r 右按钮监听器
* @return the unbind dialog
*/
public static Dialog getUnbindDialog(final Activity ctx, OnClickListener r) {
final Dialog dialog = new Dialog(ctx, R.style.noBackgroundDialog) {
};
LayoutInflater layoutInflater = LayoutInflater.from(ctx);
View v = layoutInflater.inflate(R.layout.dialog_unbind, null);
Button leftBtn = (Button) v.findViewById(R.id.left_btn);
Button rightBtn = (Button) v.findViewById(R.id.right_btn);
leftBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
dismissDialog(ctx, dialog);
}
});
rightBtn.setOnClickListener(r);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCanceledOnTouchOutside(false);
dialog.setCancelable(false);
dialog.setContentView(v);
return dialog;
}
public void onOCRClick(View v) {
try {
if (v.getId() == R.id.ocr_click) {
surfaceDestroyed(mSurfaceHolder);
// launch Ocr capture activity.
Intent intent2 = new Intent(v.getContext(), OcrCaptureActivity.class);
intent2.putExtra(OcrCaptureActivity.AutoFocus, mFocused);
intent2.putExtra(OcrCaptureActivity.UseFlash, mFlashMode);
intent2.putExtra(OcrCaptureActivity.WidgetIntent, false);
startActivity(intent2);
}
} catch (Exception e) {
Dialog d = new Dialog(this);
d.setTitle(R.string.error_oca);
TextView tv = new TextView(this);
tv.setText(e.toString());
d.setContentView(tv);
d.show();
}
}
public void showDialog(@NonNull AppCompatActivity activity) {
if (countries == null || countries.isEmpty()) {
throw new IllegalArgumentException(context.getString(R.string.error_no_countries_found));
} else {
activity.getLifecycle().addObserver(this);
dialog = new Dialog(activity);
View dialogView = activity.getLayoutInflater().inflate(R.layout.country_picker, null);
initiateUi(dialogView);
setCustomStyle(dialogView);
setSearchEditText();
setupRecyclerView(dialogView);
dialog.setContentView(dialogView);
if (dialog.getWindow() != null) {
WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
params.width = LinearLayout.LayoutParams.MATCH_PARENT;
params.height = LinearLayout.LayoutParams.MATCH_PARENT;
dialog.getWindow().setAttributes(params);
if (theme == THEME_NEW) {
Drawable background =
ContextCompat.getDrawable(context, R.drawable.ic_dialog_new_background);
if (background != null) {
background.setColorFilter(
new PorterDuffColorFilter(backgroundColor, PorterDuff.Mode.SRC_ATOP));
}
rootView.setBackgroundDrawable(background);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}
}
dialog.show();
}
}
private void AlertDialog(String url) {
dialog = new Dialog(mContext);
dialog.setContentView(R.layout.alertdialog);
dialog.setTitle("Test Capcha");
ImageView captcha = dialog.findViewById(R.id.captcha);
captcha_edit_text= dialog.findViewById(R.id.captcha_edit_text);
captcha_edit_text.requestFocus();
//InputMethodManager imm = (InputMethodManager) mContext.getSystemService(mContext.INPUT_METHOD_SERVICE);
//imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,InputMethodManager.HIDE_IMPLICIT_ONLY);
Glide.with(mContext)
.load(url).into(captcha);
Button okButton = dialog.findViewById(R.id.OKButton);
okButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
String captcha_text = captcha_edit_text.getText().toString();
String full_url = "https://api.openload.co/1/file/dl?file=" + file_id + "&ticket=" + ticket + "&captcha_response=" + captcha_text;
retrofit_2(full_url);
dialog.dismiss();
}
});
//dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
dialog.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
dialog.show();
}
public Dialog show(@NonNull View view) {
try {
Dialog dialog = new Dialog(view.getContext());
dialog.setContentView(view);
dialog.findViewById(R.id.popupButtonClose).setOnClickListener(new PopupDialogCloseListener(dialog));
dialog.show();
return dialog;
} catch (Exception e) {
// ignore: unable to show details
return null;
}
}
/**
* 设备故障无法使用,拨打客服热线 对话框.
*
* @param ctx the ctx
* @param contentStr 对话框内容
* @param r 右按钮监听器
* @return the device errir dialog
*/
public static Dialog getDeviceErrirDialog(final Activity ctx,
String contentStr, OnClickListener r) {
final Dialog dialog = new Dialog(ctx, R.style.noBackgroundDialog) {
};
LayoutInflater layoutInflater = LayoutInflater.from(ctx);
View v = layoutInflater.inflate(R.layout.dialog_alarm_for_conditioner,
null);
TextView content = (TextView) v.findViewById(R.id.fault_content);
Button leftBtn = (Button) v.findViewById(R.id.fault_left_btn);
Button rightBtn = (Button) v.findViewById(R.id.fault_right_btn);
content.setText(contentStr);
leftBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
dismissDialog(ctx, dialog);
}
});
rightBtn.setOnClickListener(r);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCanceledOnTouchOutside(false);
dialog.setCancelable(false);
dialog.setContentView(v);
return dialog;
}
private void checkPin() {
if (SettingsActivity.isPinProtected(getActivity())) {
final Dialog d = new Dialog(getActivity(), R.style.Theme_Document_DailogPIN);
View view = new PinViewHelper((LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE), null, null) {
public void onEnter(String password) {
super.onEnter(password);
if (SettingsActivity.checkPin(getActivity(), password)) {
super.onEnter(password);
SettingsActivity.setPin(getActivity(), "");
pin_set_preference.setSummary(R.string.pin_disabled);
showMsg(R.string.pin_disabled);
setInstruction(R.string.pin_disabled);
d.dismiss();
return;
}
showError(R.string.incorrect_pin);
setInstruction(R.string.incorrect_pin);
}
public void onCancel() {
super.onCancel();
d.dismiss();
}
}.getView();
view.findViewById(R.id.logo).setVisibility(View.GONE);
d.setContentView(view);
d.show();
}
else {
setPin();
}
}
public DlgChooseDirectory(Activity ctx, Result res, String startDir) {
m_context = ctx;
m_result = res;
if (startDir != null)
m_currentDir = new File(startDir);
else
m_currentDir = Environment.getExternalStorageDirectory();
listDirs(ctx);
final Dialog dialog = new Dialog(ctx, R.style.FullHeightDialog);
View view = ((Activity) ctx).getLayoutInflater().inflate(R.layout.dlg_dirlist, null);
TextView title = (TextView) view.findViewById(R.id.dlgtitle);
Button btnOk = (Button) view.findViewById(R.id.buttonOk);
editText = (EditText) view.findViewById(R.id.editText);
editText.setText(m_currentDir.toString());
title.setText(ctx.getString(R.string.dlg_choosedir_title));
m_list = (ListView) view.findViewById(R.id.listView);
DirAdapter adapter = new DirAdapter(android.R.layout.simple_list_item_1);
m_list.setAdapter(adapter);
m_list.setOnItemClickListener(this);
btnOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (m_result != null)
m_result.onChooseDirectory(editText.getText().toString());//m_currentDir.getAbsolutePath() );
dialog.dismiss();
}
});
dialog.setContentView(view);
dialog.show();
}
/**
* 预览图片
*
* @param img
* @param context
*/
public static void show(Bitmap img, Context context) {
Dialog dia = new Dialog(context, R.style.edit_AlertDialog_style2);
dia.setContentView(R.layout.activity_start_dialog);
ImageView imageView = (ImageView) dia.findViewById(R.id.start_img);
imageView.setImageBitmap(img);
dia.show();
dia.setCanceledOnTouchOutside(true); // Sets whether this dialog is
Window w = dia.getWindow();
WindowManager.LayoutParams lp = w.getAttributes();
lp.x = 0;
lp.y = 40;
dia.onWindowAttributesChanged(lp);
}
@NonNull
@Override
public final Dialog onCreateDialog(Bundle savedInstanceState) {
if (getArguments() == null) {
throw new IllegalStateException("arguments mustn't be null");
}
Dialog dialog = new Dialog(requireContext());
dialog.setContentView(R.layout.dialog_info);
mTxtTitle = dialog.findViewById(R.id.txt_title);
mTxtMessage = dialog.findViewById(R.id.txt_message);
mBtnPositive = dialog.findViewById(R.id.btn_positive);
mTxtTitle.setText(getArguments().getString(ARG_TITLE));
mTxtMessage.setText(getArguments().getString(ARG_MESSAGE));
mBtnPositive.setText(getArguments().getString(ARG_BUTTON_CAPTION));
mBtnPositive.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onButtonClicked();
}
});
return dialog;
}
/** Create the dialog where the user can choose the current layer.
@param savedInstanceState the saved instance state (not used).
*/
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
final Activity context = getActivity();
final Dialog dialog = new Dialog(context);
drawingPanel = (FidoEditor)context.findViewById(R.id.drawingPanel);
layerButton= (Button)context.findViewById(R.id.layer);
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.open_file);
final Vector<LayerDesc> layers =
drawingPanel.getDrawingModel().getLayers();
// Here we create an adapter for the list. It is the custom class
// defined in this very file.
LayerAdapter customLayerAdapter = new LayerAdapter(
context,
R.layout.layer_spinner_item,
layers);
// We associate the adapter with the
ListView list = (ListView) dialog.findViewById(R.id.fileList);
list.setAdapter(customLayerAdapter);
list.setPadding(10, 10, 10, 10);
diag=dialog;
return dialog;
}
public AlertDialog builder() {
// 获取Dialog布局
View view = LayoutInflater.from(context).inflate(
R.layout.view_alertdialog, null);
// 获取自定义Dialog布局中的控件
lLayout_bg = (LinearLayout) view.findViewById(R.id.lLayout_bg);
txt_title = (TextView) view.findViewById(R.id.txt_title);
txt_title.setVisibility(View.GONE);
txt_msg = (TextView) view.findViewById(R.id.txt_msg);
txt_msg.setVisibility(View.GONE);
btn_neg = (Button) view.findViewById(R.id.btn_neg);
btn_neg.setVisibility(View.GONE);
btn_pos = (Button) view.findViewById(R.id.btn_pos);
btn_pos.setVisibility(View.GONE);
img_line = (ImageView) view.findViewById(R.id.img_line);
img_line.setVisibility(View.GONE);
// 定义Dialog布局和参数
dialog = new Dialog(context, R.style.AlertDialogStyle);
dialog.setContentView(view);
// 调整dialog背景大小
lLayout_bg.setLayoutParams(new FrameLayout.LayoutParams((int) (display
.getWidth() * 0.85), LayoutParams.WRAP_CONTENT));
return this;
}
@Override
public void setupDialog(Dialog dialog, int style) {
//super.setupDialog(dialog, style);
View contentView = View.inflate(getContext(), R.layout.dialog_modal, null);
dialog.setContentView(contentView);
CoordinatorLayout.LayoutParams layoutParams =
(CoordinatorLayout.LayoutParams) ((View) contentView.getParent()).getLayoutParams();
CoordinatorLayout.Behavior behavior = layoutParams.getBehavior();
if (behavior != null && behavior instanceof BottomSheetBehavior) {
((BottomSheetBehavior) behavior).setBottomSheetCallback(mBottomSheetBehaviorCallback);
}
}
/**
* 底部列表弹窗
*/
public Dialog buildBottomListDialog(Activity activity, String[] items, final DialogListener listener) {
ListView listView = new ListView(activity.getApplicationContext());
listView.setDivider(new ColorDrawable(activity.getResources().getColor(R.color.color_divider)));
listView.setDividerHeight(1);
listView.setBackgroundColor(activity.getResources().getColor(R.color.white_f1));
ArrayAdapter<String> adapter = new ArrayAdapter<>(activity, R.layout.adapter_bottom_dialog_sytle1, items);
listView.setAdapter(adapter);
final Dialog bottomDialog = new Dialog(activity, R.style.BottomDialog);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
listener.onItemClick(bottomDialog, position);
}
});
bottomDialog.setContentView(listView);
ViewGroup.LayoutParams layoutParams = listView.getLayoutParams();
layoutParams.width = activity.getResources().getDisplayMetrics().widthPixels;
listView.setLayoutParams(layoutParams);
bottomDialog.getWindow().setGravity(Gravity.BOTTOM);
bottomDialog.getWindow().setWindowAnimations(R.style.BottomDialog_Animation);
return bottomDialog;
}
/**
* Shows the splash screen over the full Activity
*/
@SuppressWarnings("deprecation")
protected void showSplashScreen(final int time) {
final CordovaActivity that = this;
Runnable runnable = new Runnable() {
public void run() {
// Get reference to display
Display display = getWindowManager().getDefaultDisplay();
// Create the layout for the dialog
LinearLayout root = new LinearLayout(that.getActivity());
root.setMinimumHeight(display.getHeight());
root.setMinimumWidth(display.getWidth());
root.setOrientation(LinearLayout.VERTICAL);
root.setBackgroundColor(preferences.getInteger("backgroundColor", Color.BLACK));
root.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT, 0.0F));
root.setBackgroundResource(that.splashscreen);
// Create and show the dialog
splashDialog = new Dialog(that, android.R.style.Theme_Translucent_NoTitleBar);
// check to see if the splash screen should be full screen
if ((getWindow().getAttributes().flags & WindowManager.LayoutParams.FLAG_FULLSCREEN)
== WindowManager.LayoutParams.FLAG_FULLSCREEN) {
splashDialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
splashDialog.setContentView(root);
splashDialog.setCancelable(false);
splashDialog.show();
// Set Runnable to remove splash screen just in case
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
removeSplashScreen();
}
}, time);
}
};
this.runOnUiThread(runnable);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
switch (item.getItemId()) {
case R.id.changelog:
final Dialog popup = new Dialog(this);
popup.requestWindowFeature(Window.FEATURE_NO_TITLE);
popup.setContentView(R.layout.dialog);
TextView text1 = (TextView) popup.findViewById(R.id.text1);
text1.setText(getString(R.string.changelog_title));
TextView text2 = (TextView) popup.findViewById(R.id.text2);
text2.setText(getString(R.string.changelog));
popup.show();
Button closebutton = (Button) popup.findViewById(R.id.button2);
closebutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Close dialog
popup.dismiss();
}
});
return true;
}
return super.onOptionsItemSelected(item);
}