Java 在 android studio 项目中显示文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33891344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Displaying text in an android studio project
提问by Bryan
Trying to get a program similar to this to work on the android platform. The trouble is I'm not sure on using xml to display text/read user input. I'm fairly new to mobile software development but from the code below, I'm only trying to display the print statements and obviously get user input too.
试图让类似的程序在 android 平台上工作。问题是我不确定使用 xml 来显示文本/读取用户输入。我对移动软件开发还很陌生,但从下面的代码来看,我只是想显示打印语句,显然也能获得用户输入。
import java.util.Scanner;
import java.util.Random;
public class assignmentMSD
{
public static void main(String[] args)
{
//objects(system)
Scanner in = new Scanner(System.in);
Random rand = new Random();
//Variables used
String[] enemies = {"Zombie", "Necromorph", "Soldier", "MadMax"};
int maxEnemyHealth = 75;
int enemyAttackDamage = 25;
//Player Variables
int health = 100;
int attackDamage = 50;
int numHealthElixers = 3;
int healthPotionHeal = 30;
int healthPotionDropChance = 50;
boolean running = true;
System.out.println("Welcome to the Thunderdome");
GAME:
while(running)
{
System.out.println("--------------------------------------------------");
int enemyHealth = rand.nextInt(maxEnemyHealth);
String enemy = enemies[rand.nextInt(enemies.length)];
System.out.println(" \t####" + enemy + " has appeared#### \n");
while(enemyHealth > 0)
{
System.out.println("\t Your Health:" + health);
System.out.println("\t" + enemy + "'s Health:" + enemyHealth);
System.out.println("\n \t What would you like to do?");
System.out.println("\t 1. Attack");
System.out.println("\t 2. Drink Health Potion");
System.out.println("\t 3. Run");
String input = in.nextLine();
if(input.equals("1"))
{
int damageGiven = rand.nextInt(attackDamage);
int damageTaken = rand.nextInt(enemyAttackDamage);
enemyHealth -= damageGiven;
health -= damageTaken;
System.out.println("\t You strike the " + enemy + " for " + damageGiven + " damage.");
System.out.println("\t You take " + damageTaken + " in combat:");
if(health < 1)
{
System.out.println("\t You have taken too much damage and therefore are too weak to fight!");
break;
}
}
else if(input.equals("2"))
{
if(numHealthElixers > 0)
{
health += healthPotionHeal;
numHealthElixers--;
System.out.println("You drink a heakth potion for " + healthPotionHeal + "."
+ "\n\t You now have" + health + " HP."
+"\n\t You have " + numHealthElixers + " Health Elixers left. \n");
}
else
{
System.out.println("\t You have no health Elixer left!, You must defeat enemies for a chance to get more \n");
}
}
else if(input.equals("3"))
{
System.out.println("\t You run away from the " + enemy + "!" );
continue GAME;
}
else
{
System.out.println("\t\n Invalid Command");
}
}
if(health < 1)
{
System.out.println("You Flee from the Thunderdome, weak from battle");
break;
}
System.out.println("-------------------------------");
System.out.println(" # " + enemy + "was defeated! # ");
System.out.println(" # You have" + health + " HP left. #");
if(rand.nextInt(100) < healthPotionDropChance)
{
numHealthElixers++;
System.out.println(" # The " + enemy + " dropped a health potion #");
System.out.println(" # You now have " + numHealthElixers + " Health Elixer(s). #");
}
System.out.println("-------------------------------");
System.out.println("What would you like to do now?");
System.out.println("1. Continue Fighting");
System.out.println("2. Exit Dungeon");
String input = in.nextLine();
while(!input.equals("1") && !input.equals("2"))
{
System.out.println("invalid command");
input = in.nextLine();
}
if(input.equals("1"))
{
System.out.println("You continue through the thunderdome");
}
else if(input.equals("2"))
{
System.out.println("You exit the thunderdome a wiser man, sucessful on your quest");
break;
}
}
System.out.println(" -------------------------- ");
System.out.println(" ----Thanks for playing----");
System.out.println(" -------------------------- ");
}
}
回答by Ramonster
Yes, you generally use TextView for displaying text, and edit text for text for the user to input.
是的,您通常使用 TextView 来显示文本,并编辑文本以供用户输入。
This is a good link to look at: http://developer.android.com/training/basics/firstapp/building-ui.html
这是一个很好的链接:http: //developer.android.com/training/basics/firstapp/building-ui.html
The developer.android training articles are very good, even for a beginner. Use the xml file in the res/layout folder for your UI stuff. Android Studio even let's you see how your UI will look if you click on the Design tab at the bottom.
developer.android 培训文章非常好,即使对于初学者也是如此。将 res/layout 文件夹中的 xml 文件用于您的 UI 内容。如果您单击底部的“设计”选项卡,Android Studio 甚至可以让您查看 UI 的外观。
Side Note: You'll find it's a lot more fun than System.out.println()
旁注:你会发现它比 System.out.println() 有趣得多
回答by Jordi Castilla
Firstin android you need to define a layout. Use:
首先在android中你需要定义一个布局。用:
EditText
to replace inputs of the user (Scanner
)TextView
to replace outputs to the user (System.out.println
)
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/some_label_text"/>
<EditText
android:id="@+id/example_input"
android:hint="@string/some_hint_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
NOTE: Labels, if text is predefined, can have the text in the definition.
注意:如果文本是预定义的,标签可以在定义中包含文本。
Secondreference your objects into your java app by id like this.
第二次通过 id 将您的对象引用到您的 java 应用程序中。
EditText exampleInput = (EditText )dialogCarro.findViewById(R.id.example_input);
Thirdyou will need to add Listeners
to Button
s or EditText
s.
第三,您需要添加Listeners
到Button
s 或EditText
s。