L'affichage du texte dans android studio de projet
Essayer d'obtenir un programme similaire pour que cela fonctionne sur la plate-forme android. Le problème est que je ne suis pas sûr que sur l'utilisation de xml pour l'affichage du texte/lire la saisie de l'utilisateur. Je suis assez nouveau mobile, développement de logiciel, mais à partir du code ci-dessous, je suis le seul à essayer pour afficher les instructions d'impression, et bien sûr la saisie de l'utilisateur aussi.
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(" -------------------------- ");
}
}
- Utilisez simplement TextView pour l'affichage de texte et un EditText pour la saisie de l'utilisateur, vérifiez ceci: developer.android.com/guide/topics/ui/controls/text.html. Je recommande également quelques tutoriels vidéo sur Android de base: newthinktank.com/2014/06/android-beginners
- Vous êtes de faire un maillage ci-dessus. Et je sais que vous n'aurez même pas comprendre un seul mot de la documentation. Je vous suggère de jeter un oeil à ces youtube.com/...
Vous devez vous connecter pour publier un commentaire.
Oui, vous utilisez généralement TextView pour afficher du texte et modifier le texte pour le texte de l'utilisateur à l'entrée.
C'est un bon lien pour regarder: http://developer.android.com/training/basics/firstapp/building-ui.html
Le développeur.android de formation les articles sont de très bonne qualité, même pour un débutant.
Utiliser le fichier xml dans le res/layout dossier pour vos trucs de l'INTERFACE utilisateur. Android Studio même vous permet de voir comment votre INTERFACE utilisateur de regarder si vous cliquez sur l'onglet Conception, dans le fond.
Côté Remarque: Vous trouverez que c'est beaucoup plus amusant que de Système.out.println()
Première dans android, vous devez définir une mise en page. Utilisation:
EditText
pour remplacer les entrées de l'utilisateur (Scanner
)TextView
pour remplacer les sorties à l'utilisateur (System.out.println
)REMARQUE: les Étiquettes, si le texte est prédéfini, peut avoir le texte de la définition.
Deuxième référencer vos objets dans votre application java par id comme ceci.
Troisième vous aurez besoin d'ajouter
Auditeurs
àButton
s ouEditText
s.TextView
objet commeexampleInput
dans ma réponse et lui attribuersetText()
chaque fois que vous voulez mettre un nouveau texte