我正在制作一个每472毫秒显示一次随机数的应用程序,每次按屏幕五次,然后清除屏幕,直到再次按屏幕。现在,我只是在进行时间安排和所有工作,直到之后才需要清除屏幕。我在代码中收到错误。 “无法解析方法'generateRandomNumber()'”。我该如何解决?
我的XML:
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="0dp"
android:layout_height="0dp"
android:clickable="true"
android:onClick="perform_action"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginBottom="75dp"
android:gravity="center"
android:textSize="400sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent" />
</RelativeLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
还有我的Java:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public int perform_action(View view) {
final List< Integer > randomNumbers = new ArrayList<>();
Observable.intervalRange(0, 5, 0, 472, TimeUnit.MILLISECONDS)
.map(aLong -> {
int previous = randomNumbers.size() > 0 ? randomNumbers.get(
randomNumbers.size() - 1) : -1;
int randomNumber = generateRandomNumber();
while (randomNumber == previous) randomNumber = generateRandomNumber();
randomNumbers.add(randomNumber);
return randomNumber;
})
.observeOn(AndroidSchedulers.mainThread())
.subscribe(integer -> {
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText(String.valueOf(integer));
});
private int generateRandomNumber() {
Random r = new Random();
int minNumber = 1;
int maxNumber = 9;
return r.nextInt( ( maxNumber - minNumber ) + 1 ) + minNumber;
}
}
}
答案 0 :(得分:0)
由于perform_action
被定义为int
,因此它也需要返回一个integer
。但是,这似乎没有必要。这应该适合您的目的。由于您只是将TextView
设置为string
-int
,所以我做了一些更改:
public class MainActivity extends AppCompatActivity {
private TextView tv;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView1);
perform_action();
}
public void perform_action() {
final List<Integer> randomNumbers = new ArrayList<>();
Observable.intervalRange(0, 5, 0, 472, TimeUnit.MILLISECONDS)
.map(aLong -> {
int previous = randomNumbers.size() > 0 ? randomNumbers.get(
randomNumbers.size() - 1) : -1;
int randomNumber = generateRandomNumber();
while (randomNumber == previous) randomNumber = generateRandomNumber();
randomNumbers.add(randomNumber);
return randomNumber;
})
.observeOn(AndroidSchedulers.mainThread())
.subscribe(integer -> {
tv.setText(String.valueOf(integer));
});
}
private int generateRandomNumber() {
Random r = new Random();
int minNumber = 1;
int maxNumber = 9;
return r.nextInt((maxNumber - minNumber) + 1) + minNumber;
}
}
您可以根据需要进行更改。