我正在读取从XML文件(通过PullParser)到AlertDialog的字符串。我需要能够在AlertDialog中显示多行文本。正常建议(例如here)是使用\ n来表示换行符。 Another thread说“应该有效”。好吧,它没有。
这是AlertDialog的代码,其中completionString是取自XML的字符串:
public void gameComplete()
{
AlertDialog alert = new AlertDialog.Builder(this).create();
myTimer.cancel();
myTimer = null;
alert.setMessage(completionString);
alert.setCancelable(true);
alert.setCanceledOnTouchOutside(false);
alert.setButton(AlertDialog.BUTTON_NEUTRAL, "OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which)
{
finish();
}
});
alert.show();
}
但这是实际屏幕截图的样子:
我是否在字符串中使用字符串中的一个或两个斜杠(\ n或\\ n),AlertDialog从字面上理解它,只打印那些确切的字符,而不是新行。如何让它显示实际的新行?
如果问题出在pull解析器上,这里是将此字符串从XML文件中提取到completionString变量的代码:
private void parseXml()
{
// Post1: tuningVars contains the values in the XML tuning file corresponding to the difficulty settings for this particular game.
// Post2: completion, win, bonus, fail strings are filled out from the endgamestrings.xml file,
// matching the digit specified by the QR/input code.
XmlPullParser theParser = Xml.newPullParser();
try {
// Post1
// Specify the XML file to be parsed.
InputStream theStream = getApplicationContext().getAssets().open("tuning.xml");
theParser.setInput(theStream, null);
// Parse!
int eventType = theParser.getEventType();
String gameTitle = "";
String tagName;
int[] difficultyNums = new int[5];
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
tagName = theParser.getName();
Log.i("XML Parser", "Got a tag called " + tagName);
if (tagName.equals("game")) {
gameTitle = theParser.getAttributeValue(null, "title");
Log.i("XML Parser", "Found a game title tag " + gameTitle + " and this game is " + extras.getString(GAME_NAME));
}
if (gameTitle.equals(extras.getString(GAME_NAME))) {
Log.i("XML Parser", "Adding variables for " + gameTitle);
while (eventType != XmlPullParser.END_TAG) {
String nextTagName = "";
if (theParser.getName() != null) {
nextTagName = theParser.getName();
}
Log.i("XML Parser", "Next tag name is" + nextTagName);
if (nextTagName.equals("gamevar")) {
String varName = theParser.getAttributeValue(null, "varname");
difficultyNums[0] = Integer.parseInt(theParser.getAttributeValue(null, "easyval"));
difficultyNums[1] = Integer.parseInt(theParser.getAttributeValue(null, "mediumval"));
difficultyNums[2] = Integer.parseInt(theParser.getAttributeValue(null, "hardval"));
difficultyNums[3] = Integer.parseInt(theParser.getAttributeValue(null, "hackerinc"));
difficultyNums[4] = Integer.parseInt(theParser.getAttributeValue(null, "languageinc"));
// then construct the Tuning variable and add it to the array
TuningVariable varToAdd = new TuningVariable(varName, difficultyNums);
tuningVars.add(varToAdd);
Log.i("XML Parser", "Added tuning variable " + varName);
}
eventType = theParser.next();
}
} else {
Log.i("XML Parser", "I compared " + extras.getString(GAME_NAME) + " with " + gameTitle + " and decided they were not the same.");
}
}
eventType = theParser.next();
}
// Post2
// Specify the XML file to be parsed.
theStream = getApplicationContext().getAssets().open("endgamestrings.xml");
theParser.setInput(theStream, null);
int chosenDigit = extras.getInt(STRINGS_NUM);
Log.i("XML Parser", "Running PullParser on endgamestrings.");
// Parse!
eventType = theParser.getEventType();
int stringNum = -1;
String parsedGameType;
String selectedGameType;
// Define which games are accelerators and which puzzles.
// It would probably make more sense to do this via the XML as well, but let's start here for testing.
switch (extras.getString((GAME_NAME))) {
case "Simon":
selectedGameType = "accelerator";
break;
case "Square":
selectedGameType = "accelerator";
break;
case "Pegs":
selectedGameType = "puzzle";
break;
case "Pipes":
selectedGameType = "puzzle";
break;
case "Lights":
selectedGameType = "puzzle";
}
while (eventType != XmlPullParser.END_DOCUMENT)
{
// Log.i("XML Parser", "Running PullParser while loop on endgamestrings.");
if (eventType == XmlPullParser.START_TAG)
{
tagName = theParser.getName();
// Log.i("XML Parser", "Got a tag called " + tagName);
if (tagName.equals("setofstrings"))
{
stringNum = Integer.parseInt(theParser.getAttributeValue(null, "id"));
parsedGameType = theParser.getAttributeValue(null, "type");
}
if (stringNum == chosenDigit)
{
/*
// Java is whining like a bitch about these variables not being initialized.
// Won't bother with this particular error-catching for now.
if (parsedGameType.equals("puzzle") && selectedGameType.equals("accelerator"))
{
String warning = "Warning: Accelerator strings passed to puzzle. Setting strings to default.";
Toast toast = Toast.makeText(GameActivity.this, warning, 10);
toast.show();
}
*/
while (eventType != XmlPullParser.END_TAG)
{
String nextTagName = "";
String stringType = "";
if (theParser.getName() != null)
{
nextTagName = theParser.getName();
}
if (nextTagName.equals("string"))
{
stringType = theParser.getAttributeValue(null, "type");
Log.i("XML Parser", "Found a string type tag " + stringType);
}
switch (stringType)
{
case ("time"):
completionString = theParser.nextText();
break;
case ("bonus"):
bonusString = theParser.nextText();
break;
case ("win"):
winString = theParser.nextText();
break;
case ("fail"):
failString = theParser.nextText();
break;
}
eventType = theParser.next();
}
}
}
eventType = theParser.next();
}
}
catch (XmlPullParserException e)
{
Log.e("XML Parser", "Dude! XmlPullParserException!");
e.printStackTrace();
}
catch (IOException e)
{
Log.e("XML Parser", "Dude! IOException!");
e.printStackTrace();
}
return;
}
答案 0 :(得分:2)
试试这个:
alert.setMessage(completionString.replace("\\n", "\n"));