【发布时间】:2017-03-22 01:12:40
【问题描述】:
尝试在一个类中传递一个随机生成的变量以在另一个类中使用,以同步我的应用程序的背景和另一个片段中的标题。
public class LoginMain extends AppCompatActivity {
RelativeLayout loginMain;
Random rand = new Random();
int bgPick = rand.nextInt(5) + 1; //distribute int from 1 to 3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_main);
//must be called after the content view is set.
loginMain = (RelativeLayout) findViewById(R.id.activity_login_main);
randomBG();//method that determines the background image based on the bgPick value.
}
public static int getBgPick(){
return bgPick;
}
主类:
public class MainActivity extends AppCompatActivity {
AppBarLayout appBarLayout;
int bgPick = LoginMain.getBgPick();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
appBarLayout = (AppBarLayout) findViewById(R.id.appbar);
randomHeader();//method to determine header image based on bgPick value determined in the LoginMain class.
这导致在每个类中分别生成和使用两个 bgPick 值。
【问题讨论】:
标签: java android variables random