我陷入两难境地。 我为java创建了这个随机句生成器脚本。我想在文本视图中显示“句子”,就像你将它安装到android studio第25行的系统一样。
这是我的随机句子生成器代码
package com.tech.littlest.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.util.Random;
public class Activity2 extends AppCompatActivity {
final static int NO_DEDICATED_ARRAYS = 12; // This number is equal to the number of arrays in a string NOTE: This number MUST be constant across all strings
final static int NO_OUTPUT_SENTENCES = 1; // How many sentences generated and displayed for the user
final static String SPACER = " "; // Inputs a " " character (Space) into the display
final static String PERIOD = ".";
static Random r = new Random();
public static void main( String args[] ){
String proposition_user[] = { "You should build and invention that", "You should build a robot that", "You should create a whatchamacallit that", "There should be a device that", "Create a futuristic thing that", "Make a thing that", "Make something cool that", "I don't recall there being a thing that", "Create a sick thing that", "Devise a plan to create an object that", "I want a thing that", "If I just had an thingamajig that", "Create a thingimajig that"};
String plural_noun[] = { "children", "chickens", "the ocean", "cars", "apples", "houses", "water", "buildings", "people", "your friends", "farms", "the human race" };
String verb[] = { "directs you to", "drives on", "manages", "develops", "makes","eats", "helps", "relocates", "fixes", "feeds", "runs on", "washes" };
String sentence;
for (int i = 0; i < NO_OUTPUT_SENTENCES; i++){
sentence = proposition_user[rand()];
char c = sentence.charAt(0);
sentence = sentence.replace( c, Character.toUpperCase(c) );
sentence += SPACER;
sentence += (verb[rand()]);
sentence += (SPACER + plural_noun[rand()]);
sentence += PERIOD;
sentence += "";
System.out.println(sentence);
}
}
static int rand(){
int ri = r.nextInt() % NO_DEDICATED_ARRAYS;
if ( ri < 0 )
ri += NO_DEDICATED_ARRAYS;
return ri;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
}
}
我的带有textview的XML文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
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="com.tech.littlest.myapplication.Activity2">
<TextView
android:id="@+id/mytextview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
谢谢!
答案 0 :(得分:0)
从我从代码中收集的内容中,您可以通过其id获取对textView的引用。
TextView textView = findViewById(R.id.myTextView);
你会把它放在onCreate方法中,因为那是你应用的起点。老实说,我不知道为什么你在活动中有一个静态主方法。该代码应该在一个不是主要的单独方法中,然后在代码中引用要打印的textView,然后说:
textView.setText(sentence);
答案 1 :(得分:0)
您正在使用此方法作为您的计划的切入点!
public static void main( String args[] ){
...
...
}
如果您使用的是Android,而您的课程从AppCompatActivity
延伸,那么您需要方法
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
...
}
加载setContentView()
的布局必须包含您的TextView
。然后获取引用并设置文本:
TextView textView = findViewById(R.id.myTextView);
textView.setText(sentence);