当前位置: 首页 > 知识库问答 >
问题:

Textview未显示字符串

郑桐
2023-03-14

我有代码在这里生成一个随机的名字给定音节点击一个动作按钮。但作为字符串currentName生成的名称会显示在TextView中。我需要使视图成为它自己的类吗?我是androidstudio的新手,来自eclipse,所以textviews对我来说也是新手。多谢了。

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = findViewById(R.id.fab);
    TextView myTextView = (TextView) findViewById(R.id.view);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            class NewName {

                String[] firstNameSyllables = new String[] { "mon", "fay", "shi", "zag", "blarg", "rash", "izen" };
                String[] lastNameSyllables = new String[] { "malo", "zak", "abo", "wonk" };

                public String createNewFirstName() {
                    String firstName = "";
                    int numberOfSyllablesInFirstName = randomRange(2, 4);

                    for (int i = 0; i < numberOfSyllablesInFirstName; i++) {
                        firstName += firstNameSyllables[randomRange(0, firstNameSyllables.length)];
                    }
                    String firstNameLetter = "";
                    firstNameLetter = firstName.substring(0, 1);
                    firstName = firstName.substring(1);
                    firstNameLetter = firstNameLetter.toUpperCase();
                    firstName = firstNameLetter + firstName;
                    return firstName;
                }

                public String createNewName() {
                    String firstName = "";
                    int numberOfSyllablesInFirstName = randomRange(2, 4);
                    for (int i = 0; i < numberOfSyllablesInFirstName; i++) {
                        firstName += firstNameSyllables[randomRange(0, firstNameSyllables.length)];
                    }
                    String firstNameLetter = "";
                    firstNameLetter = firstName.substring(0, 1);
                    firstName = firstName.substring(1);
                    firstNameLetter = firstNameLetter.toUpperCase();
                    firstName = firstNameLetter + firstName;
                    String lastName = "";
                    int numberOfSyllablesInLastName = randomRange(1, 3);
                    for (int j = 0; j < numberOfSyllablesInLastName; j++) {
                        lastName += lastNameSyllables[randomRange(0, lastNameSyllables.length)];
                    }
                    String lastNameLetter = "";
                    lastNameLetter = lastName.substring(0, 1);
                    lastName = lastName.substring(1);
                    lastNameLetter = lastNameLetter.toUpperCase();
                    lastName = lastNameLetter + lastName;
                    String currentName = firstName + " " + lastName;
                    myTextView.setText(currentName);
                   return currentName;



                }

                public int randomRange(int min, int max) {
                    Random random = new Random();
                    return random.nextInt(max - min) + min;






                }




            }
        }
    });
}

}

这也是我的activity_main.xml

xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/Theme.NameGenerator.AppBarOverlay">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/Theme.NameGenerator.PopupOverlay" />

</com.google.android.material.appbar.AppBarLayout>

<include layout="@layout/content_main" />

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|center|center_horizontal|end"
    android:layout_margin="@dimen/fab_margin"
    android:contentDescription="@string/make_a_name_appear"
    android:rotationY="178"
    app:srcCompat="@drawable/abc_vector_test" />

<TextView
    android:id="@+id/view"
    android:layout_width="201dp"
    android:layout_height="67dp"
    android:translationX="150dp"
    android:translationY="500dp" />

共有1个答案

林英朗
2023-03-14

单击时将newname类拉出,然后创建它的实例,然后访问该方法。因此您的mainactivity将如下所示。

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = findViewById(R.id.fab);
        TextView myTextView = (TextView) findViewById(R.id.view);
        //create instance
        NewName newName = new NewName();
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //call method and set text
                myTextView.setText(newName.createNewName());
            }
        });
    }

    class NewName {

        String[] firstNameSyllables = new String[]{"mon", "fay", "shi", "zag", "blarg", "rash", "izen"};
        String[] lastNameSyllables = new String[]{"malo", "zak", "abo", "wonk"};

        public String createNewFirstName() {
            String firstName = "";
            int numberOfSyllablesInFirstName = randomRange(2, 4);

            for (int i = 0; i < numberOfSyllablesInFirstName; i++) {
                firstName += firstNameSyllables[randomRange(0, firstNameSyllables.length)];
            }
            String firstNameLetter = "";
            firstNameLetter = firstName.substring(0, 1);
            firstName = firstName.substring(1);
            firstNameLetter = firstNameLetter.toUpperCase();
            firstName = firstNameLetter + firstName;
            return firstName;
        }

        public String createNewName() {
            String firstName = "";
            int numberOfSyllablesInFirstName = randomRange(2, 4);
            for (int i = 0; i < numberOfSyllablesInFirstName; i++) {
                firstName += firstNameSyllables[randomRange(0, firstNameSyllables.length)];
            }
            String firstNameLetter = "";
            firstNameLetter = firstName.substring(0, 1);
            firstName = firstName.substring(1);
            firstNameLetter = firstNameLetter.toUpperCase();
            firstName = firstNameLetter + firstName;
            String lastName = "";
            int numberOfSyllablesInLastName = randomRange(1, 3);
            for (int j = 0; j < numberOfSyllablesInLastName; j++) {
                lastName += lastNameSyllables[randomRange(0, lastNameSyllables.length)];
            }
            String lastNameLetter = "";
            lastNameLetter = lastName.substring(0, 1);
            lastName = lastName.substring(1);
            lastNameLetter = lastNameLetter.toUpperCase();
            lastName = lastNameLetter + lastName;
            String currentName = firstName + " " + lastName;
            return currentName;


        }

        public int randomRange(int min, int max) {
            Random random = new Random();
            return random.nextInt(max - min) + min;

        }

    }


}

我还从文本中删除了translation属性以进行检查,您可以根据您的要求进行操作:

<TextView
        android:id="@+id/view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="cdc"
        android:textColor="#a00" />
 类似资料:
  • 我试图在m文本视图中呈现希伯来文文本。 我在这里读到,我需要将默认android的字体更改为支持希伯来语的字体。 我尝试了几个解决方案,但都没有成功: 或: 这是我的xml: 有没有办法解决这个问题? 有没有办法为每个应用程序定义默认字体?per Layout?

  • 我正在创建一个包含希腊字符的XHTML。在下面找到一个简单的例子。 当我打印结果时,我得到了。 有什么能帮忙的吗?

  • 我刚刚开始尝试Android应用程序开发,所以我决定尝试一下Android自己的教程(这个:http://developer.android.com/training/basics/firstapp/starting-activity.html) 我的新活动中的文本视图不会显示。这是我的代码:

  • 问题内容: 我想打印一个字符或字符串,例如’-‘n次。 我可以不使用循环就做吗? ..这意味着打印3次,如下所示: 问题答案: Python 2.x: Python 3.x:

  • 我使用下面的代码强制我的textview以英文数字显示其文本数字:

  • 我正在将一个包含一些数字(全部用波斯语)的字符串加载到android文本视图中。一切都很好,直到我改变了我的自定义字体,文本的数字显示为英文数字。 我知道我的新字体支持波斯语数字。当我使用正确显示的数字下方的代码更改数字区域设置时。 问题是我有一个字符串,很难找到数字部分并改变它。我以前的字体工作正常,我不明白这个字体有什么问题。 你知道如何全局解决所有文本视图,或者至少一个字符串的问题吗?