文本显示在Android Studio项目 [英] Displaying text in an android studio project

查看:376
本文介绍了文本显示在Android Studio项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图得到类似这样的程序在Android平台上工作。麻烦的是我不知道如何使用XML来显示文本/读取用户输入。我是相当新的移动软件开发,但是从下面的code,我只是想显示打印报表,显然获取用户输入了。

 进口java.util.Scanner中;
    进口了java.util.Random;    公共类assignmentMSD
    {
        公共静态无效的主要(字串[] args)
        {
            //对象(系统)
            扫描仪在=新的扫描仪(System.in);
            随机兰特=新的随机();        //使用的变量        的String [] =敌人{僵尸,Necromorph,战士,MadMax};
        INT maxEnemyHealth = 75;
        INT enemyAttackDamage = 25;        //播放器变数
        INT健康= 100;
        INT attackDamage = 50;
        INT numHealthElixers = 3;
        INT healthPotionHeal = 30;
        INT healthPotionDropChance = 50;        布尔运行= TRUE;        的System.out.println(欢迎来到Thunderdome);        游戏:
        而(运行)
        {
            的System.out.println(------------------------------------------- -------);            INT enemyHealth = rand.nextInt(maxEnemyHealth);
            字符串敌人的敌人= [rand.nextInt(enemies.length)];
            的System.out.println(\\ t ####+ +敌人已出现#### \\ n);            而(enemyHealth大于0)
            {
                的System.out.println(\\ t您的健康:+健康);
                的System.out.println(\\ t+敌+的健康:+ enemyHealth);
                的System.out.println(\\ n \\ t你想怎么办?);
                的System.out.println(\\ t 1.攻击);
                的System.out.println(\\ t 2.饮料卫生药水);
                的System.out.println(\\ t 3.运行);                字符串输入= in.nextLine();
                                如果(input.equals(1))
                                {
                                   INT damageGiven = rand.nextInt(attackDamage);
                                   INT damageTaken = rand.nextInt(enemyAttackDamage);                                   enemyHealth - = damageGiven;
                                   健康 - = damageTaken;                                   的System.out.println(\\ t您罢工+ +敌为+ damageGiven +的伤害。);
                                   的System.out.println(\\ t你拿+ damageTaken +战斗:);                                   如果(健康< 1)
                                   {
                                       的System.out.println(\\ t您已经迈出太大的伤害,因此是太弱打!);
                                       打破;                                   }
                                }
                                否则如果(input.equals(2))
                                {
                                   如果(numHealthElixers大于0)
                                   {
                                       健康+ = healthPotionHeal;
                                       numHealthElixers--;
                                       的System.out.println(你喝了药水heakth为+ healthPotionHeal +。
                                                            +\\ n \\ t你现在有+健康+HP。
                                                            +\\ n \\ t你有+ numHealthElixers +健康酏离开\\ n);                                   }
                                   其他
                                   {
                                       的System.out.println(\\ t你有没有留下健康酏剂!你必须打败敌人,即有机会获得更多的\\ n);                                   }
                                }
                                否则如果(input.equals(3))
                                {
                                    的System.out.println(\\ t你跑离敌人+ +!);
                                    继续游戏;
                                }
                                其他
                                {
                                    的System.out.println(\\ t \\ n无效的命令);
                                }
            }                        如果(健康< 1)
                        {
                            的System.out.println(你从Thunderdome出逃,从战场弱);
                            打破;
                        }                        的System.out.println(-------------------------------);
                        的System.out.println(#+敌人+被打败了#!);
                        的System.out.println(#你有+健康+HP左#);
                        如果(rand.nextInt(100)下; healthPotionDropChance)
                        {
                            numHealthElixers ++;
                            的System.out.println(#中的+敌人+下降了健康药水#);
                            的System.out.println(#你现在有+ numHealthElixers +健康酏剂(S)#。);                        }
                        的System.out.println(-------------------------------);
                        的System.out.println(你会想现在做什么?);
                        的System.out.println(1继续战斗。);
                        的System.out.println(2退出地牢。);                        字符串输入= in.nextLine();                        而(input.equals(1)及!&放大器;!input.equals(2))
                        {
                            的System.out.println(无效的命令);
                            输入= in.nextLine();                        }                        如果(input.equals(1))
                        {
                            的System.out.println(你继续通过thunderdome);
                        }
                        否则如果(input.equals(2))
                        {
                            的System.out.println(你退出thunderdome一智,你的追求SUCESSFUL);
                            打破;
                        }
        }                的System.out.println(--------------------------);
                的System.out.println(----谢谢----玩);
                的System.out.println(--------------------------);
    }
}


解决方案

第一在Android中,你需要定义一个布局。用途:

 <的TextView
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:文字=@字符串/ some_label_text/>
<的EditText
    机器人:ID =@ + ID / EXAMPLE_INPUT
    机器人:提示=@字符串/ some_hint_text
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT/>

请注意:标签,如果文本predefined,可以在定义文本

引用你的对象到您的Java应用程序由ID是这样的。

 的EditText exampleInput =(EditText上)dialogCarro.findViewById(R.id.example_input);

第三您将需要添加<一个href=\"http://stackoverflow.com/questions/5941960/how-to-implement-a-listener\"><$c$c>Listeners按钮 s或的EditText 秒。

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.

    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(" -------------------------- ");


    }
}

解决方案

First in android you need to define a layout. Use:

  • 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.

Second reference your objects into your java app by id like this.

EditText exampleInput = (EditText )dialogCarro.findViewById(R.id.example_input);

Third you will need to add Listeners to Buttons or EditTexts.

这篇关于文本显示在Android Studio项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆