【问题标题】:Displaying text in an android studio project在 android studio 项目中显示文本
【发布时间】:2016-02-26 18:11:02
【问题描述】:

试图让与此类似的程序在 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(" -------------------------- ");


    }
}

【问题讨论】:

标签: java android xml android-studio


【解决方案1】:

是的,你一般用TextView来显示文字,编辑文字供用户输入。

这是一个很好的链接:http://developer.android.com/training/basics/firstapp/building-ui.html

developer.android 培训文章非常好,即使对于初学者也是如此。 将 res/layout 文件夹中的 xml 文件用于您的 UI 内容。如果您单击底部的“设计”选项卡,Android Studio 甚至可以让您查看 UI 的外观。

旁注:你会发现它比 System.out.println() 有趣得多

【讨论】:

  • 如果我将文本转换为字符串并将其放在文本视图中会怎样?
【解决方案2】:

首先在android中你需要定义一个布局。使用:

  • EditText 替换用户的输入 (Scanner)
  • TextView 替换输出给用户 (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" />

注意:标签,如果文本是预定义的,可以在定义中包含文本。

第二次通过像这样的 id 将您的对象引用到您的 java 应用程序中。

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

第三您需要将Listeners 添加到Buttons 或EditTexts。

【讨论】:

  • 我是否必须将每个 system.out.print 替换为新的文本视图,或者可以全部从一个文本视图中完成
  • 取决于您的要求,但在标准流程中,一个必须足够
  • 目前只是为了让它在 android 上运行。我得到了关于将文本转换为字符串并将其放入文本视图的建议,但这需要多个文本视图。如果目前有一种方法可以在一个文本视图中显示所有内容,我想那将是一个更好的选择。
  • 为什么要多个?只需在我的答案中获取TextView 对象,如exampleInput,并在每次要放置新文本时将其分配给setText()
  • 每次用户进行选择时,文本都会重复自身(用户选择的选项 1、2、3),因此如果我分配 setText(),它将在用户进行选择后再次显示选择?
猜你喜欢
  • 2021-08-07
  • 2016-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多