下面列出了android.app.AlertDialog#findViewById ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void testValueEditDialogPositive() {
int newValue = 10;
mNumericOptionItem.setLabel(getString(R.string.msdkui_violate_truck_options));
final TextView valueView = mNumericOptionItem.findViewById(R.id.numeric_item_value);
assertNotNull(valueView);
valueView.performClick();
AlertDialog alertDialog = (AlertDialog) ShadowDialog.getLatestDialog();
assertTrue(alertDialog.isShowing());
EditText valueEdit = alertDialog.findViewById(R.id.numeric_item_value_text);
valueEdit.setText(String.valueOf(newValue));
alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick();
assertEquals(newValue, mNumericOptionItem.getValue().intValue());
}
@Override
public void onClick(View view) {
if (!activity.isFinishing()) {
String text = FileUtils.readFile(activity.getResources(), resourceId);
AlertDialog alertDialog = new AlertDialog
.Builder(view.getContext())
.setTitle(titleId)
.setMessage(text)
.setNeutralButton(android.R.string.ok, new Close())
.create();
alertDialog.show();
if (isSmallFont) {
TextView textView = alertDialog.findViewById(android.R.id.message);
textView.setTextSize(8);
}
}
}
/**
* Show a model dialog box. The <code>android.app.AlertDialog</code> object is returned so that
* you can specify an OnDismissListener (or other listeners) if required.
* <b>Note:</b> show() is already called on the AlertDialog being returned.
*
* @param context The current Context or Activity that this method is called from.
* @param message Message to display in the dialog.
* @return AlertDialog that is being displayed.
*/
public static AlertDialog quickDialog(final Activity context, final String message) {
final SpannableString s = new SpannableString(message); //Make links clickable
Linkify.addLinks(s, Linkify.ALL);
final Builder builder = new AlertDialog.Builder(context);
builder.setMessage(s);
builder.setPositiveButton(android.R.string.ok, closeDialogListener());
AlertDialog dialog = builder.create();
if(!context.isFinishing()) {
dialog.show();
final TextView textView = (TextView) dialog.findViewById(android.R.id.message);
if (textView != null) {
textView.setMovementMethod(LinkMovementMethod.getInstance()); //Make links clickable
}
}
return dialog;
}
public void onShow(DialogInterface dialogInterface) {
final AlertDialog dialog = (AlertDialog) dialogInterface;
final RecyclerView listView = dialog.findViewById(android.R.id.list);
listView.setAdapter(mAdapter);
if (mDividerDecoration == null) {
mDividerDecoration = new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL);
mDividerDecoration.setDoNotDrawForLastItem(true);
}
listView.addItemDecoration(mDividerDecoration);
mAdapter.submitList(mData);
}
@Test
public void testCreateDirectoryDialogDisallowFolderNameModification() {
final String directoryName = "mydir";
final DirectoryChooserFragment fragment = DirectoryChooserFragment.newInstance(
DirectoryChooserConfig.builder()
.newDirectoryName(directoryName)
.initialDirectory("")
.allowReadOnlyDirectory(false)
.allowNewDirectoryNameModification(false)
.build());
startFragment(fragment, DirectoryChooserActivityMock.class);
fragment.onOptionsItemSelected(new TestMenuItem() {
@Override
public int getItemId() {
return R.id.new_folder_item;
}
});
final AlertDialog dialog = (AlertDialog) ShadowDialog.getLatestDialog();
final ShadowAlertDialog shadowAlertDialog = Shadows.shadowOf(dialog);
assertThat(shadowAlertDialog.getTitle()).isEqualTo("Create folder");
assertThat(ShadowDialog.getShownDialogs()).contains(dialog);
final TextView msgView = (TextView) dialog.findViewById(R.id.msgText);
assertThat(msgView).hasText("Create new folder with name \"mydir\"?");
final EditText editText = (EditText) dialog.findViewById(R.id.editText);
assertThat(editText).isGone();
}
/**
* Show an app installation alert, in case you need to do that yourself.
* @param activity calling Activity
*/
public static void showAppInstallAlert(final Activity activity){
String appName=null;
try{
PackageManager pm=activity.getPackageManager();
appName=pm.getApplicationLabel(pm.getApplicationInfo(activity.getPackageName(), 0)).toString().replace("<", "<");
}catch(PackageManager.NameNotFoundException ignore){}
ImageView banner=new ImageView(activity);
banner.setImageResource(R.drawable.telegram_logo_large);
banner.setBackgroundColor(0xFF4fa9e6);
float dp=activity.getResources().getDisplayMetrics().density;
int pad=Math.round(34*dp);
banner.setPadding(0, pad, 0, pad);
LinearLayout content=new LinearLayout(activity);
content.setOrientation(LinearLayout.VERTICAL);
content.addView(banner);
TextView alertText=new TextView(activity);
alertText.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
alertText.setTextColor(0xFF000000);
alertText.setText(Html.fromHtml(activity.getString(R.string.PassportSDK_DownloadTelegram, appName).replaceAll("\\*\\*([^*]+)\\*\\*", "<b>$1</b>")));
alertText.setPadding(Math.round(24*dp), Math.round(24*dp), Math.round(24*dp), Build.VERSION.SDK_INT<Build.VERSION_CODES.LOLLIPOP ? Math.round(24*dp) : Math.round(2*dp));
content.addView(alertText);
AlertDialog alert=new AlertDialog.Builder(activity, /*Build.VERSION.SDK_INT<Build.VERSION_CODES.LOLLIPOP ? AlertDialog.THEME_HOLO_LIGHT :*/ R.style.Theme_Telegram_Alert)
.setView(content)
.setPositiveButton(R.string.PassportSDK_OpenGooglePlay, new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which){
activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=org.telegram.messenger")));
}
})
.setNegativeButton(R.string.PassportSDK_Cancel, null)
.show();
if(Build.VERSION.SDK_INT<Build.VERSION_CODES.LOLLIPOP){
int titleDividerId=activity.getResources().getIdentifier("titleDivider", "id", "android");
View titleDivider=alert.findViewById(titleDividerId);
if(titleDivider!=null){
titleDivider.setVisibility(View.GONE);
}
}
}
/**
* Sets file name in the text field of a dialog.
*
* @param alertDialog the dialog
* @param destinationName the id of the EditText
* @param name the name
*/
public void presetNameForDialog(AlertDialog alertDialog, @IdRes int destinationName, String name) {
EditText editText = alertDialog.findViewById(destinationName);
if (editText != null) {
editText.setText(name);
} else {
Log.w(TAG, "Unable to find view, can not set file title.");
}
}
/**
* LoadingDialog
*/
public AlertDialog getLoadingDialog(String text) {
final AlertDialog dlg = new AlertDialog
.Builder(new ContextThemeWrapper(mContext, R.style.lib_pub_dialog_style))
.create();
if (mContext instanceof Activity && !((Activity) mContext).isFinishing()) {
dlg.show();
}
dlg.setContentView(R.layout.lib_pub_dialog_loading);
TextView tips = (TextView) dlg.findViewById(R.id.tv_tips);
if (text != null) {
tips.setText(text);
}
return dlg;
}
protected void launchThanks(int which) {
String thankYouMsg = getResources().getString(R.string.donate_thanks);
// if (which == THANK_YOU_PAYPAL)
// thankYouMsg += "\n\n" + getResources().getString(R.string.donate_thanks_paypal);
AlertDialog alert = new AlertDialog.Builder(Settings.this)
.setTitle(R.string.donate_thanks_title)
.setIcon(R.drawable.ic_logo)
.setMessage(thankYouMsg)
.setPositiveButton(R.string.donate_thanks_continue, null)
.show();
TextView msgTxt = (TextView) alert.findViewById(android.R.id.message);
msgTxt.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
}
@SuppressLint("NewApi")
private void changeTextDirection(Builder dlg){
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
dlg.create();
AlertDialog dialog = dlg.show();
if (currentapiVersion >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
TextView messageview = (TextView)dialog.findViewById(android.R.id.message);
messageview.setTextDirection(android.view.View.TEXT_DIRECTION_LOCALE);
}
}
public void testQuickDialog(){
AlertDialog alertDialog = DialogUtils.quickDialog(getActivity(), "Test Message");
assertTrue(alertDialog.isShowing());
final TextView textView = (TextView) alertDialog.findViewById(android.R.id.message);
assertEquals("Test Message", textView.getText().toString());
alertDialog.dismiss();
assertFalse(alertDialog.isShowing());
}
@SuppressLint("NewApi")
private void changeTextDirection(Builder dlg){
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
dlg.create();
AlertDialog dialog = dlg.show();
if (currentapiVersion >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
TextView messageview = (TextView)dialog.findViewById(android.R.id.message);
messageview.setTextDirection(android.view.View.TEXT_DIRECTION_LOCALE);
}
}
/**
* Prepare for the glReadPixels test.
*/
public ReadPixelsTask(AlertDialog dialog, int resultTextId,
int width, int height, int iterations) {
mDialog = dialog;
mResultTextId = resultTextId;
mWidth = width;
mHeight = height;
mIterations = iterations;
mProgressBar = (ProgressBar) dialog.findViewById(R.id.work_progress);
mProgressBar.setMax(mIterations);
}
private void updateTextView(String newText, int idOfViewToUpdate) {
AlertDialog dialog = (AlertDialog)getDialog();
if (dialog != null) {
TextView tv = dialog.findViewById(idOfViewToUpdate);
tv.setText(newText);
}
}
private void prepareInputDialog(final AlertDialog dialog) {
dialog.setTitle(getName());
dialog.setIcon(getIcon());
final View input = getInputView();
FrameLayout fl = dialog.findViewById(android.R.id.custom);
fl.removeAllViews();
fl.addView(input, new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
View.OnClickListener clickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
if (view == dialog.getButton(DialogInterface.BUTTON_POSITIVE)
&& !validateAndSetInput(input)) {
return;
}
dialog.dismiss();
}
};
dialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(clickListener);
dialog.getButton(DialogInterface.BUTTON_NEGATIVE).setOnClickListener(clickListener);
final MeasurementView next = getNextView();
if (next != null) {
dialog.getButton(DialogInterface.BUTTON_NEUTRAL).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (validateAndSetInput(input)) {
next.prepareInputDialog(dialog);
}
}
});
}
else {
dialog.getButton(DialogInterface.BUTTON_NEUTRAL).setVisibility(GONE);
}
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final File file = new File(getArguments().getString(Constants.SOURCE_FILE));
LayoutInflater inflater = LayoutInflater.from(getActivity());
String theme = PreferenceManager.getDefaultSharedPreferences(getActivity()).getString(getString(R.string.pref_theme_key), "");
AlertDialog.Builder dialogBuilder = setUpDialog(file, inflater, theme);
AlertDialog dialog = dialogBuilder.show();
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
newNameField = (EditText) dialog.findViewById(R.id.new_name);
return dialog;
}
@SuppressLint("NewApi")
private void changeTextDirection(Builder dlg){
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
dlg.create();
AlertDialog dialog = dlg.show();
if (currentapiVersion >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
TextView messageview = (TextView)dialog.findViewById(android.R.id.message);
messageview.setTextDirection(android.view.View.TEXT_DIRECTION_LOCALE);
}
}
@SuppressLint("NewApi")
private void changeTextDirection(Builder dlg){
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
dlg.create();
AlertDialog dialog = dlg.show();
if (currentapiVersion >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
TextView messageview = (TextView)dialog.findViewById(android.R.id.message);
messageview.setTextDirection(android.view.View.TEXT_DIRECTION_LOCALE);
}
}
@SuppressLint("NewApi")
private void changeTextDirection(Builder dlg){
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
dlg.create();
AlertDialog dialog = dlg.show();
if (currentapiVersion >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
TextView messageview = (TextView)dialog.findViewById(android.R.id.message);
messageview.setTextDirection(android.view.View.TEXT_DIRECTION_LOCALE);
}
}
@Test
public void testCreateDirectoryDialogAllowFolderNameModification() {
final String directoryName = "mydir";
final DirectoryChooserFragment fragment = DirectoryChooserFragment.newInstance(
DirectoryChooserConfig.builder()
.newDirectoryName(directoryName)
.initialDirectory("")
.allowReadOnlyDirectory(false)
.allowNewDirectoryNameModification(true)
.build());
startFragment(fragment, DirectoryChooserActivityMock.class);
fragment.onOptionsItemSelected(new TestMenuItem() {
@Override
public int getItemId() {
return R.id.new_folder_item;
}
});
final AlertDialog dialog = (AlertDialog) ShadowDialog.getLatestDialog();
final ShadowAlertDialog shadowAlertDialog = Shadows.shadowOf(dialog);
assertThat(shadowAlertDialog.getTitle()).isEqualTo("Create folder");
assertThat(ShadowDialog.getShownDialogs()).contains(dialog);
final TextView msgView = (TextView) dialog.findViewById(R.id.msgText);
assertThat(msgView).hasText("Create new folder with name \"mydir\"?");
final EditText editText = (EditText) dialog.findViewById(R.id.editText);
assertThat(editText).isVisible();
assertThat(editText).hasTextString(directoryName);
}