我试图显示屏幕上生成的随机数,但出于某种原因它不会出现在屏幕上看起来像
_______
1 2 3
4 5 6
7 8 9
del 0 # -
随机数将显示“ _ ”
的位置main.xml
<TextView
android:id="@+id/Title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:text=" "
android:textSize="45dp" />
question.Java
package org.example.question;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class QuestionActivity extends Activity implements View.OnClickListener {
/** Called when the activity is first created. */
int fnum, snum;
Button one,two,three,four,five,six,seven,eight,nine,zero,minus,hash;
TextView display;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
display = (TextView)findViewById(R.id.Title);
//Buttons
one= (Button) findViewById(R.id.keypad_1);
two= (Button) findViewById(R.id.keypad_2);
three= (Button) findViewById(R.id.keypad_3);
four= (Button) findViewById(R.id.keypad_4);
five = (Button) findViewById(R.id.keypad_5);
six= (Button) findViewById(R.id.keypad_6);
seven = (Button) findViewById(R.id.keypad_7);
eight = (Button) findViewById(R.id.keypad_8);
nine = (Button) findViewById(R.id.keypad_9);
minus = (Button) findViewById(R.id.keypad_subtract);
hash = (Button) findViewById(R.id.keypad_hash);
one.setOnClickListener(this); two.setOnClickListener(this); three.setOnClickListener(this);
three.setOnClickListener(this); four.setOnClickListener(this); five.setOnClickListener(this);
six.setOnClickListener(this); seven.setOnClickListener(this); eight.setOnClickListener(this);
nine.setOnClickListener(this); minus.setOnClickListener(this); hash.setOnClickListener(this);
}
public void onClick(View arg0) {
// TODO Auto-generated method stub
display.setText("2+2");
}
public void requestFocus() {
// TODO Auto-generated method stub
}
}
帮助将非常感激
答案 0 :(得分:1)
您可以使用java.util.Random类生成随机数。 例如:
Random r = new Random();
int randomInt = r.nextInt(10);
这将生成0到9范围内的随机数。您可以将此代码放在按钮的onClick()中并将其设置为TextView。