textview没有显示字符串

deikduxw  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(316)

我在这里有代码生成一个随机名称给定的音节点击一个行动按钮。但是作为字符串currentname生成的名称会显示在textview中。我需要让视图成为自己的类还是什么?我是来自eclipse的androidstudio的新手,所以文本视图对我来说是新的。谢谢。

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\u 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" />
pdsfdshx

pdsfdshx1#

拉你的手 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" />

相关问题