[英]Android Studio: Making a random number generator app, random number doesn't display?
I have EditViews to prompt the user for a min and max. 我有EditViews来提示用户最小和最大。 The data entered is passed into an Activity function and a random number is generated. 输入的数据被传递到活动函数中,并生成一个随机数。 That random number is passed onto another Activity to be displayed. 该随机数将传递到另一个要显示的活动。 When I enter a min number and a max number and then click the button that says generate, I get directed to a blank screen. 当我输入一个最小数字和一个最大数字,然后单击显示生成的按钮时,我被定向到空白屏幕。 However, I'm expecting randValue to be displayed. 但是,我希望显示randValue。
MainActivity.java MainActivity.java
public class MainActivity extends AppCompatActivity {
public final static String RAND_NUM = "com.example.random.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void generateNum(View view) {
Intent intent = new Intent(this, DisplayNumActivity.class);
EditText minInput = (EditText) findViewById(R.id.min_num);
EditText maxInput = (EditText) findViewById(R.id.max_num);
int minNum = Integer.parseInt(minInput.getText().toString());
int maxNum = Integer.parseInt(maxInput.getText().toString());
Random rand = new Random();
int randNum = rand.nextInt(maxNum - minNum) + minNum;
intent.putExtra(RAND_NUM, randNum);
startActivity(intent);
}
}
DisplayNumActivity.java DisplayNumActivity.java
public class DisplayNumActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_num);
Intent intent = getIntent();
String randValue = intent.getStringExtra(MainActivity.RAND_NUM);
TextView textView = new TextView(this);
textView.setTextSize(30);
textView.setText(randValue);
ViewGroup layout = (ViewGroup) findViewById(R.id.activity_display_num);
layout.addView(textView);
}
}
activity_main.xml activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context="com.example.random.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Min number"
android:id="@+id/textView"
android:layout_marginTop="40dp"
android:layout_alignParentTop="true"
android:layout_alignLeft="@+id/textView2"
android:layout_alignStart="@+id/textView2" />
<EditText
android:id="@+id/min_num"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="min"
android:inputType="number"
android:layout_marginTop="32dp"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Max number"
android:id="@+id/textView2"
android:layout_marginTop="43dp"
android:layout_below="@+id/min_num"
android:layout_centerHorizontal="true" />
<EditText
android:id="@+id/max_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="max"
android:inputType="number"
android:minWidth="500dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_text"
android:onClick="generateNum"
android:id="@+id/button"
android:layout_below="@+id/max_num"
android:layout_centerHorizontal="true"
android:layout_marginTop="43dp" />
OK, two things: 好,两件事:
1) In DisplayNumActivity, change 1)在DisplayNumActivity中,更改
String randValue = intent.getStringExtra(MainActivity.RAND_NUM);
to this: 对此:
int randValue = intent.getIntExtra(MainActivity.RAND_NUM, 0);
Here we are reading in an int value from the intent instead of a String. 在这里,我们从意图而不是字符串中读取一个int值。 In your main activity you saved an int here, not a String, the String here is just the "key" in the key-value pair. 在您的主要活动中,您在此处保存了一个整数,而不是字符串,这里的字符串只是键值对中的“键”。 The zero is just a default in case it can't find your int. 零只是一个默认值,以防找不到您的整数。
2) To display the number you have to first change that int back to a String in order to set it in a TextView and then we display it to your relative layout like this: 2)要显示数字,您必须首先将int改回String以便在TextView中进行设置,然后将其显示为您的相对布局,如下所示:
String stringRandValue = Integer.toString(randValue);
View relativeLayout = findViewById(R.id.activity_display_num);;
TextView textView = new TextView(this);
textView.setTextSize(30);
textView.setText(stringRandValue);
((RelativeLayout)relativeLayout).addView(textView);
That should work. 那应该工作。
I think you have to specify exactly which layout you want to add your textview to. 我认为您必须确切指定要将textview添加到的布局。
Like: RelativeLayout relativelayout = (RelativeLayout)findViewById(R.id.relativelayoutID); 像这样: RelativeLayout relativelayout =(RelativeLayout)findViewById(R.id.relativelayoutID);
Casting to viewgroup seems bit heavy to me. 对我来说,投射到视图组似乎有些沉重。 Please try suggested way and let me know if it changes anything for you. 请尝试建议的方式,让我知道它是否为您带来任何改变。
Edit : There is one more thing which I feel might be the culprit in this case and that is missing layout params for your text View. 编辑 :在这种情况下,我觉得还有另外一件事可能是罪魁祸首,并且您的文本视图缺少布局参数。
Example: //create params like this 示例: //创建这样的参数
LayoutParams layoutparams = new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT );
//set layout params like this //像这样设置布局参数
textView.setLayoutParams(layoutparams);
Note - replace the parent layout type according to your xml where this textView is been added. 注意-根据添加了textView的xml替换父布局类型。
Replace this 取代这个
String randValue = intent.getStringExtra(MainActivity.RAND_NUM); 字符串randValue = intent.getStringExtra(MainActivity.RAND_NUM);
With this 有了这个
String randValue = intent.getIntExtra(MainActivity.RAND_NUM, default_int_value); 字符串randValue = intent.getIntExtra(MainActivity.RAND_NUM,default_int_value);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.