我正在开发一个应用程序,我使用材质设计对话框向用户显示一些信息,我想要的是在对话框中使文本样式的一部分变为粗体。如何实现这个问题。
这是我对话框的图片: - enter image description here
这里我想要这个手机号码文字样式粗体和颜色
这是我的代码: -
AlertDialog.Builder builder = new AlertDialog.Builder(CRegistrationScreen.this);
builder.setCancelable(false);
builder.setMessage("We will send you a verification SMS to this Number.\n\n"+s_szResponseMobileNum+"\n\nIs this OK,or would you like to edit the number.");
builder.setPositiveButton(R.string.ok, null);
builder.setNegativeButton(R.string.edit,null);
builder.show();
答案 0 :(得分:2)
对于特定样式文字,我们希望将文字转换为 HTML 并使用 HTML TAG 进行样式设置
尝试这可能是有效的....................
String sourceString = "We will send you a verification SMS to this Number.<br><br> <b><font color='#08b608'>" + s_szResponseMobileNum + "</b> <br><br>Is this OK,or would you like to edit the number." ;
builder.setMessage(Html.fromHtml(sourceString));
享受编码.....................
答案 1 :(得分:0)
你基本上可以在字符串资源中使用html标签,如:
<resource>
<string id="@+id/no">We will send you a verification SMS to this Number <b>your mobile no</b>this OK,or would you like to edit the number</string>
</resources>
答案 2 :(得分:0)
我正在给你一步一步指南
第1步
在gradle文件中添加支持lib
dependencies {
compile 'com.android.support:appcompat-v7:X.X.X' // where X.X.X version
}
第2步
让您的活动延长android.support.v7.app.AppCompatActivity.
public class MainActivity extends AppCompatActivity {
...
}
第3步创建警告对话框
private void showLocationDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle(getString(R.string.dialog_title));
builder.setMessage(Html.fromHtml(""+"<b>"+R.string.dialog_message+"</b>"));
String positiveText = getString(android.R.string.ok);
builder.setPositiveButton(positiveText,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// positive button logic
}
});
String negativeText = getString(android.R.string.cancel);
builder.setNegativeButton(negativeText,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// negative button logic
}
});
AlertDialog dialog = builder.create();
// display dialog
dialog.show();
}
第4步
为对话框背景声明自定义 drawable.xml 。
<?xml version="1.0" encoding="utf-8"?>
<!-- From: support/v7/appcompat/res/drawable/abc_dialog_material_background_light.xml -->
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetLeft="16dp"
android:insetTop="16dp"
android:insetRight="16dp"
android:insetBottom="16dp">
<shape android:shape="rectangle">
<corners android:radius="2dp" />
<solid android:color="@color/indigo" />
</shape>
</inset>
在 style.xml 文件
中声明样式<style name="MyDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<!--buttons color-->
<item name="colorAccent">@color/pink</item>
<!--title and message color-->
<item name="android:textColorPrimary">@android:color/white</item>
<!--dialog background-->
<item name="android:windowBackground">@drawable/background_dialog</item>
</style>
第5步 最后一步
创建对话框并使用样式作为AlertDialog.Builder中的参数。
AlertDialog.Builder builder =
new AlertDialog.Builder(this, R.style.MyDialogTheme);
...
AlertDialog dialog = builder.create();
// display dialog
dialog.show();
答案 3 :(得分:0)
您可以通过以下代码解决您的查询,这将帮助您以粗体显示特定颜色的数字。
您可以使用Spannable类,尝试以下代码。
String strMessage = “We will send you a verification SMS to this Number.\n\n” + s_szResponseMobileNum + "\n\nIs this OK,or would you like to edit the number."
Spannable wordtoSpan = new SpannableString(strMessage);
wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), strMessage.indexOf(s_szResponseMobileNum), s_szResponseMobileNum.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
wordtoSpan.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), strMessage.indexOf(s_szResponseMobileNum), s_szResponseMobileNum.length, 0);
AlertDialog.Builder builder = new AlertDialog.Builder(CRegistrationScreen.this);
builder.setCancelable(false);
builder.setMessage(wordtoSpan);
builder.setPositiveButton(R.string.ok, null);
builder.setNegativeButton(R.string.edit,null);
builder.show();
希望这会奏效。
答案 4 :(得分:0)
您可以使用StyleSpan
将文字的特定部分设为粗体。
SpannableStringBuilder ssb = new SpannableStringBuilder();
ssb.append("We will send you a verification SMS to this Number:\n\n");
int start = ssb.length();
ssb.append(phoneNumber);
ssb.append("\n\nIs this OK, or would you like to edit the number?");
ssb.setSpan(new StyleSpan(Typeface.BOLD), start, phoneNumber.length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
AlertDialog.Builder builder = new AlertDialog.Builder(CRegistrationScreen.this);
builder.setCancelable(false)
.setMessage(ssb)
.setPositiveButton(R.string.ok, null)
.setNegativeButton(R.string.edit, null)
.show();