如何在应用程序中拥有多个独立布局(屏幕)时更改布局视图?

huangapple go评论99阅读模式
英文:

How do I change layout view on the application when i have multiple separate layouts(screens)?

问题

Here's the translated code you provided:

MainActivity layout xml - screen 1

  1. <!-- ... XML layout content ... -->

FoundNum layout xml - screen 2

  1. <!-- ... XML layout content ... -->

MainActivity Java

  1. package com.example.findrandomnumber;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.util.Log;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.widget.RelativeLayout;
  8. import android.widget.TextView;
  9. import android.widget.Toast;
  10. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  11. private Button buttonstart, buttonplus, buttonminus, buttoncheck, buttonrestart, buttonrestart1;
  12. private TextView TVcurrentnum, TVshowmessage, tvcounter, tvfoundnum;
  13. private String TAG = "gilog";
  14. private Controllerguessnum myC;
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState) {
  17. // ... Java code ...
  18. }
  19. // ... Other methods ...
  20. public void changepage() {
  21. // ... Java code ...
  22. }
  23. @Override
  24. public void onClick(View v) {
  25. // ... Java code ...
  26. }
  27. }

Controllerguessnum Java

  1. package com.example.findrandomnumber;
  2. public class Controllerguessnum {
  3. // ... Java code ...
  4. }

Modelguessnum Java

  1. package com.example.findrandomnumber;
  2. import java.util.Random;
  3. public class Modelguessnum {
  4. // ... Java code ...
  5. }

Based on the error you're encountering, it seems like the issue is related to changing the layout using setContentView. To resolve the error, make sure that the layout you're trying to set as content (either page or grats layout) is properly inflated and not null.

If you want the layout to change when you find the number, you should use an Intent to navigate to the next activity (or in your case, the second layout) instead of changing the content view within the same activity. This approach is more standard for transitioning between different screens/layouts.

Also, ensure that you handle the initialization of views and other components correctly in both layout XMLs and the Java code to avoid null references.

英文:

xml mainactivity screen 1

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;RelativeLayout
  3. xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  4. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  5. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  6. android:layout_width=&quot;match_parent&quot;
  7. android:layout_height=&quot;match_parent&quot;
  8. tools:context=&quot;.MainActivity&quot;
  9. android:background=&quot;#00008b&quot;
  10. android:id=&quot;@+id/page&quot;
  11. &gt;
  12. &lt;TextView
  13. android:id=&quot;@+id/game&quot;
  14. android:layout_width=&quot;wrap_content&quot;
  15. android:layout_height=&quot;wrap_content&quot;
  16. android:text=&quot;The Random Game&quot;
  17. android:textStyle=&quot;bold|italic&quot;
  18. android:layout_centerInParent=&quot;true&quot;
  19. android:layout_alignParentTop=&quot;true&quot;
  20. app:layout_constraintEnd_toEndOf=&quot;parent&quot;
  21. app:layout_constraintStart_toStartOf=&quot;parent&quot;
  22. android:textColor=&quot;#006400&quot;
  23. android:outlineSpotShadowColor=&quot;#2196F3&quot;
  24. app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
  25. &lt;Button
  26. android:id=&quot;@+id/buttonstart&quot;
  27. android:text=&quot;Start&quot;
  28. android:textStyle=&quot;bold|italic&quot;
  29. android:layout_width=&quot;wrap_content&quot;
  30. android:layout_height=&quot;wrap_content&quot;
  31. android:layout_below=&quot;@id/game&quot;
  32. android:layout_centerInParent=&quot;true&quot;
  33. android:textColor=&quot;#006400&quot;
  34. android:background=&quot;#8b0000&quot;
  35. /&gt;
  36. &lt;TextView
  37. android:id=&quot;@+id/TVshowmessage&quot;
  38. android:text=&quot;the number is&quot;
  39. android:layout_width=&quot;wrap_content&quot;
  40. android:layout_height=&quot;wrap_content&quot;
  41. android:layout_below=&quot;@id/buttonstart&quot;
  42. android:layout_centerInParent=&quot;true&quot;
  43. android:textColor=&quot;#006400&quot;
  44. /&gt;
  45. &lt;Button
  46. android:id=&quot;@+id/buttonplus&quot;
  47. android:text=&quot;+&quot;
  48. android:textStyle=&quot;bold|italic&quot;
  49. android:textSize=&quot;@android:dimen/app_icon_size&quot;
  50. android:layout_width=&quot;wrap_content&quot;
  51. android:layout_height=&quot;wrap_content&quot;
  52. android:layout_below=&quot;@id/TVshowmessage&quot;
  53. android:layout_centerInParent=&quot;true&quot;
  54. android:textColor=&quot;#006400&quot;
  55. android:background=&quot;#8b0000&quot;
  56. /&gt;
  57. &lt;TextView
  58. android:id=&quot;@+id/TVcurrentnum&quot;
  59. android:text=&quot;0&quot;
  60. android:layout_width=&quot;wrap_content&quot;
  61. android:layout_height=&quot;wrap_content&quot;
  62. android:layout_below=&quot;@id/buttonplus&quot;
  63. android:layout_centerInParent=&quot;true&quot;
  64. android:textColor=&quot;#006400&quot;
  65. android:background=&quot;#8b0000&quot;
  66. /&gt;
  67. &lt;Button
  68. android:id=&quot;@+id/buttonminus&quot;
  69. android:text=&quot;-&quot;
  70. android:textSize=&quot;@android:dimen/app_icon_size&quot;
  71. android:layout_width=&quot;wrap_content&quot;
  72. android:layout_height=&quot;wrap_content&quot;
  73. android:layout_below=&quot;@id/TVcurrentnum&quot;
  74. android:layout_centerInParent=&quot;true&quot;
  75. android:textColor=&quot;#006400&quot;
  76. android:background=&quot;#8b0000&quot;
  77. /&gt;
  78. &lt;TextView
  79. android:id=&quot;@+id/tvcounter&quot;
  80. android:text=&quot;counter&quot;
  81. android:layout_width=&quot;wrap_content&quot;
  82. android:layout_height=&quot;66dp&quot;
  83. android:layout_centerInParent=&quot;true&quot;
  84. android:layout_marginBottom=&quot;300dp&quot;
  85. android:background=&quot;#8b0000&quot;
  86. android:textColor=&quot;#006400&quot;
  87. android:textStyle=&quot;bold|italic&quot;
  88. android:textAlignment=&quot;center&quot;
  89. android:layout_below=&quot;@id/buttonrestart&quot;
  90. /&gt;
  91. &lt;Button
  92. android:id=&quot;@+id/buttoncheck&quot;
  93. android:layout_width=&quot;wrap_content&quot;
  94. android:layout_height=&quot;wrap_content&quot;
  95. android:layout_centerInParent=&quot;true&quot;
  96. android:layout_centerHorizontal=&quot;true&quot;
  97. android:textColor=&quot;#006400&quot;
  98. android:background=&quot;#8b0000&quot;
  99. android:textStyle=&quot;bold|italic&quot;
  100. android:outlineSpotShadowColor=&quot;#008B02&quot;
  101. android:text=&quot;Check&quot;
  102. android:layout_below=&quot;@id/buttonminus&quot;
  103. /&gt;
  104. &lt;Button
  105. android:id=&quot;@+id/buttonrestart&quot;
  106. android:text=&quot;restart&quot;
  107. android:layout_width=&quot;wrap_content&quot;
  108. android:layout_height=&quot;wrap_content&quot;
  109. android:layout_centerInParent=&quot;true&quot;
  110. android:textColor=&quot;#006400&quot;
  111. android:background=&quot;#8b0000&quot;
  112. android:textStyle=&quot;bold|italic&quot;
  113. android:layout_below=&quot;@id/buttoncheck&quot;
  114. /&gt;
  115. &lt;/RelativeLayout&gt;

foundnum xml screen 2

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;RelativeLayout
  3. xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; android:layout_width=&quot;match_parent&quot;
  4. android:layout_height=&quot;match_parent&quot;
  5. android:background=&quot;#00008b&quot;
  6. android:id=&quot;@+id/grats&quot;
  7. &gt;
  8. &lt;TextView
  9. android:id=&quot;@+id/tvfound&quot;
  10. android:layout_width=&quot;wrap_content&quot;
  11. android:layout_height=&quot;wrap_content&quot;
  12. android:text=&quot;congrats&quot;
  13. android:textColor=&quot;#006400&quot;
  14. android:background=&quot;#8b0000&quot;
  15. android:textStyle=&quot;bold|italic&quot;
  16. android:layout_above=&quot;@+id/buttonrestart1&quot;
  17. android:layout_centerInParent=&quot;true&quot;
  18. /&gt;
  19. &lt;Button
  20. android:id=&quot;@+id/buttonrestart1&quot;
  21. android:text=&quot;restart&quot;
  22. android:layout_width=&quot;wrap_content&quot;
  23. android:layout_height=&quot;wrap_content&quot;
  24. android:layout_centerInParent=&quot;true&quot;
  25. android:textColor=&quot;#006400&quot;
  26. android:background=&quot;#8b0000&quot;
  27. android:textStyle=&quot;bold|italic&quot;
  28. /&gt;
  29. &lt;/RelativeLayout&gt;

main activity java.

  1. package com.example.findrandomnumber;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.util.Log;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.widget.RelativeLayout;
  8. import android.widget.TextView;
  9. import android.widget.Toast;
  10. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  11. private Button buttonstart, buttonplus, buttonminus, buttoncheck, buttonrestart,buttonrestart1;
  12. private TextView TVcurrentnum, TVshowmessage, tvcounter,tvfoundnum;
  13. private String TAG = &quot;gilog&quot;,currentnum,counter;
  14. private Controllerguessnum myC;
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState) {
  17. Log.d(TAG, &quot;onCreate: hi&quot;);
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.activity_main);
  20. buttonstart =(Button) findViewById(R.id.buttonstart);
  21. buttonplus = (Button)findViewById(R.id.buttonplus);
  22. buttonminus =(Button) findViewById(R.id.buttonminus);
  23. buttoncheck = (Button)findViewById(R.id.buttoncheck);
  24. buttonrestart = (Button)findViewById(R.id.buttonrestart);
  25. buttonrestart1 = (Button)findViewById(R.id.buttonrestart1);
  26. TextView tvcounter= findViewById(R.id.tvcounter);
  27. TextView TVshowmessage= findViewById(R.id.TVshowmessage);
  28. TextView TVcurrentnum= findViewById(R.id.TVcurrentnum);
  29. TextView tvfoundnum= findViewById(R.id.tvfound);
  30. findViewById(R.id.page);
  31. findViewById(R.id.grats);
  32. buttonstart.setOnClickListener(this);
  33. buttonrestart.setOnClickListener(this);
  34. buttoncheck.setOnClickListener(this);
  35. buttonplus.setOnClickListener(this);
  36. buttonminus.setOnClickListener(this);
  37. Log.d(TAG, &quot;onCreate: set clickers worked&quot;);
  38. }
  39. public void startGame() {
  40. myC = new Controllerguessnum();
  41. Log.d(TAG, &quot;startGame: random number is &quot; + myC.crandom());
  42. }
  43. public void showmessage(){
  44. TextView TVshowmessage= findViewById(R.id.TVshowmessage);
  45. Log.d(TAG, &quot;onClick: button check was pressed&quot;);
  46. if (myC.ccheck() == &quot;smaller&quot;){
  47. Log.d(TAG, &quot;onClick: check is ? &quot;+ myC.ccheck() +&quot; numbers: &quot;+myC.ccurrentnum()+ &quot;&lt;&quot;+ myC.crandom());
  48. TVshowmessage.setText(&quot;number is smaller than random number&quot; + 0);
  49. }
  50. else
  51. if (myC.ccheck() == &quot;equal&quot;){
  52. Log.d(TAG, &quot;onClick: check is ? &quot;+ myC.ccheck() + &quot;=&quot;+ myC.crandom());
  53. TVshowmessage.setText(&quot;congrats you found the number, &quot;+String.valueOf(myC.ccounter()/2)+&quot; tries,&quot;+ &quot; press restart to play again&quot; );
  54. }
  55. else {
  56. if(myC.ccheck() == &quot;bigger&quot;) {
  57. Log.d(TAG, &quot;onClick: check is ? &quot;+ myC.ccheck() + &quot;&gt;&quot;+ myC.crandom());
  58. TVshowmessage.setText(&quot;number you chose is bigger than random number&quot; + 0);
  59. }
  60. }
  61. }
  62. public void changepage(){
  63. TextView tvfoundnum= findViewById(R.id.tvfound);
  64. RelativeLayout grats= findViewById(R.id.grats);
  65. RelativeLayout page= findViewById(R.id.page);
  66. TextView TVshowmessage= findViewById(R.id.TVshowmessage);
  67. if (myC.ccheck() == &quot;equal&quot;){
  68. page.setVisibility(View.GONE);
  69. setContentView(grats);
  70. }
  71. else{
  72. setContentView(page);
  73. }
  74. }
  75. @Override
  76. public void onClick(View v) {
  77. buttonstart =(Button) findViewById(R.id.buttonstart);
  78. buttonplus = (Button)findViewById(R.id.buttonplus);
  79. buttonminus =(Button) findViewById(R.id.buttonminus);
  80. buttoncheck = (Button)findViewById(R.id.buttoncheck);
  81. buttonrestart = (Button)findViewById(R.id.buttonrestart);
  82. TVcurrentnum= findViewById(R.id.TVcurrentnum);
  83. tvcounter= findViewById(R.id.tvcounter);
  84. if (v.getId() == R.id.buttonstart || v.getId() == R.id.buttonrestart) {
  85. startGame();
  86. }
  87. if (v.getId() == R.id.buttonrestart1){
  88. findViewById(R.id.page).setVisibility(View.VISIBLE);
  89. findViewById(R.id.grats).setVisibility(View.GONE);
  90. startGame();
  91. }
  92. //button check
  93. if (v.getId() == R.id.buttoncheck) {
  94. showmessage();
  95. myC.ccntplus();
  96. tvcounter.setText(String.valueOf((int)(myC.ccounter())+ 0));
  97. Log.d(TAG, &quot;onClick: counter returned&quot;+ tvcounter.getText());
  98. changepage();
  99. }
  100. if (v.getId() == R.id.buttonplus) {
  101. Log.d(TAG, &quot;onClick: button plus was pressed&quot;);
  102. myC.cplus();
  103. TVcurrentnum.setText(String.valueOf((int)(myC.ccurrentnum()) + 0));
  104. } else if (v.getId() == R.id.buttonminus) {
  105. Log.d(TAG, &quot;onClick: button minus was pressed&quot;);
  106. myC.cminus();
  107. TVcurrentnum.setText(String.valueOf((int)(myC.ccurrentnum()) + 0));
  108. }
  109. }
  110. }

there is more code in two other java files but these are not relevant to changing screen but I will add it below.
controller.java

  1. package com.example.findrandomnumber;
  2. public class Controllerguessnum {
  3. private Modelguessnum myModel1;
  4. public double counter=0;
  5. private int random;
  6. public Controllerguessnum(){
  7. myModel1= new Modelguessnum();
  8. cstartGame();
  9. this.counter=0;
  10. }
  11. public void cstartGame(){
  12. myModel1.mStartGame();
  13. this.counter=0; }
  14. public void crestartgame(){
  15. myModel1.mStartGame();
  16. this.counter=0;
  17. }
  18. public void cplus(){
  19. myModel1.mplus();
  20. }
  21. public void cminus(){
  22. myModel1.mminus();
  23. }
  24. public void ccntplus(){
  25. this.counter++;
  26. }
  27. public String ccheck(){
  28. String res = myModel1.mcheck();
  29. return res;
  30. }
  31. public double ccurrentnum(){
  32. int cnum=myModel1.getCurrentnum();
  33. return cnum;
  34. }
  35. public double ccounter(){
  36. double cnt= this.counter;
  37. return cnt;
  38. }
  39. public int crandom(){
  40. this.random = myModel1.getRandomnum();
  41. return this.random;
  42. }
  43. }

model.java

  1. package com.example.findrandomnumber;
  2. import java.util.Random;
  3. public class Modelguessnum {
  4. private int currentnum;
  5. private int randomnum;
  6. private int counter=0;
  7. public Modelguessnum() {
  8. }
  9. public void mStartGame(){
  10. // this.randomnum= new Random().nextInt( 100);
  11. // this.currentnum= new Random().nextInt(100);
  12. this.randomnum=5;
  13. this.currentnum=5;
  14. }
  15. public int getCurrentnum(){
  16. int mnum= currentnum;
  17. return mnum;
  18. }
  19. public int getRandomnum(){
  20. return randomnum;
  21. }
  22. public void mplus(){
  23. this.currentnum++;
  24. }
  25. public void mminus()
  26. {
  27. this.currentnum--;
  28. }
  29. public String mcheck() {
  30. //-1 is smaller
  31. //0 equal
  32. //1 is bigger
  33. if (this.currentnum &gt; this.randomnum) {
  34. return &quot;bigger&quot;;
  35. }
  36. else
  37. if (this.currentnum==this.randomnum){
  38. return &quot;equal&quot;;
  39. }
  40. else
  41. if(this.currentnum &lt; this.randomnum){
  42. return &quot;smaller&quot;;
  43. }
  44. else
  45. return &quot;neither&quot;;
  46. }
  47. }

the math part of the code worked fine- any improvements are welcome. - how do I solve the errors so that when I find the number, when the random number = current number and when I press check, I want it to bring me to the second screen/layout(foundnum.xml) - foundnum.xml
the error I get from this is the following:

  1. E/AndroidRuntime: FATAL EXCEPTION: main
  2. Process: com.example.findrandomnumber, PID: 480
  3. java.lang.IllegalArgumentException: Cannot add a null child view to a ViewGroup
  4. at android.view.ViewGroup.addView(ViewGroup.java:3718)
  5. at android.view.ViewGroup.addView(ViewGroup.java:3700)
  6. at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:687)
  7. at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:175)
  8. at com.example.findrandomnumber.MainActivity.changepage(MainActivity.java:95)
  9. at com.example.findrandomnumber.MainActivity.onClick(MainActivity.java:129)
  10. at android.view.View.performClick(View.java:4780)
  11. at android.view.View$PerformClick.run(View.java:19866)
  12. at android.os.Handler.handleCallback(Handler.java:739)
  13. at android.os.Handler.dispatchMessage(Handler.java:95)
  14. at android.os.Looper.loop(Looper.java:135)
  15. at android.app.ActivityThread.main(ActivityThread.java:5254)
  16. at java.lang.reflect.Method.invoke(Native Method)
  17. at java.lang.reflect.Method.invoke(Method.java:372)
  18. at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
  19. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)&gt;

答案1

得分: 1

异常是:无法将空的子视图添加到ViewGroup

ViewGroup是一个可以包含其他视图的视图。ViewGroup是Android中布局的基类,例如LinearLayout、RelativeLayout、FrameLayout等。换句话说,ViewGroup通常用于定义在Android屏幕上设置/排列/列出视图(小部件)的布局。

你有两个ViewGroup:一个是activity_main.xml,另一个是foundnum.xml

在你的代码(MainActivity)中,你通过这行代码setContentView(R.layout.activity_main);activity_main.xml设置为你的ViewGroup,放在你的onCreate方法中。

是什么导致了这个错误?
这个错误是由对tvfoundnumbuttonrestart1进行操作(设置文本或设置点击操作)引起的:

  • TextView tvfoundnum = findViewById(R.id.tvfound)
  • buttonrestart1 = (Button)findViewById(R.id.buttonrestart1);

你对它们设置的任何操作都会导致错误,因为这些视图不是设置的ViewGroup的一部分。换句话说,它们不包含在activity_main.xml中,也还未被识别。

解决方案选项:
有两种方法可以实现你想要的效果:

  1. 通过隐藏和显示只想要显示的视图:这意味着你需要将foundnum.xml的代码合并到activity_main.xml中,从而获得一个布局。
  2. 使用片段(Fragments)。使用片段,一个片段可以管理多个ViewGroup(布局)。

由于你正在使用Activity,我将演示解决方案选项(1)如下:

解决方案一:
这是新的布局(它包含了activity_main.xmlfoundnum.xml的代码):activity_main.xml

  1. <!-- 代码已被省略,见上文 -->

注意,当你运行应用程序时,请注意有些视图是隐藏的,即:

  1. <RelativeLayout>
  2. <!-- 页面的相对布局 --> 这是可见的(可见性为 visible )
  3. <!-- 祝贺的相对布局 --> 这是不可见的(可见性为 gone )
  4. </RelativeLayout>

在完成你想要的操作后(检查数字是否在计数器中……),将<!-- 祝贺的相对布局 -->的可见性设置为可见,将<!-- 页面的相对布局 -->的可见性设置为隐藏。

你需要修改changepage()函数中的if语句,类似于这样:

  1. public void changepage(){
  2. if (myC.ccheck().equals("equal")){
  3. page.setVisibility(View.GONE);
  4. grats.setVisibility(View.VISIBLE);
  5. } else {
  6. grats.setVisibility(View.GONE);
  7. page.setVisibility(View.VISIBLE);
  8. }
  9. }

希望你会发现这个有帮助。如果有问题,请留言,我会帮助你解决。

英文:

The exception you getting is that you: Cannot add a null child view to a ViewGroup.

A ViewGroup is a view that can contain other views. The ViewGroup is the base class for Layouts in android, like LinearLayout , RelativeLayout , FrameLayout etc. In other words, ViewGroup is generally used to define the layout in which views(widgets) will be set/arranged/listed on the android screen.

You have two ViewGroups: one activity_main.xml and foundnum.xml

Within your code (MainActivity), you set activity_main.xml as your viewGroup by this line of code setContentView(R.layout.activity_main); in your oncreate method.

What causes the error??
The error is caused by taking action(setting text or setting onclick action) on tvfoundnum and buttonrestart1:

  • TextView tvfoundnum= findViewById(R.id.tvfound)
  • buttonrestart1 = (Button)findViewById(R.id.buttonrestart1);

Any action you set to them will result into an error because those views are not part of the set ViewGroup. In otherwards they are not contained in activity_main.xml and they are not yet known

Solution Options:
There are 2 ways you can be able to achieve what you want:

  1. By hiding and displaying only the views you want to display: Meaning you will have to combine foundnum.xml's code to activity_main.xml. Such that you have one layout.
  2. By using fragments. With fragments, you can have one fragment manage more than one ViewGroup (layouts).

Since you are using activity, I will demonstrate solution option (1) bellow

Solution Option One.
This is the new layout(it contains code from activity_main.xml & foundnum.xml): activity_main.xml.

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  3. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  4. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  5. android:layout_width=&quot;match_parent&quot;
  6. android:layout_height=&quot;match_parent&quot;
  7. tools:context=&quot;.MainActivity&quot;
  8. &gt;
  9. &lt;RelativeLayout
  10. android:id=&quot;@+id/page&quot;
  11. android:layout_width=&quot;match_parent&quot;
  12. android:layout_height=&quot;match_parent&quot;
  13. &gt;
  14. &lt;TextView
  15. android:id=&quot;@+id/game&quot;
  16. android:layout_width=&quot;wrap_content&quot;
  17. android:layout_height=&quot;wrap_content&quot;
  18. android:layout_alignParentTop=&quot;true&quot;
  19. android:layout_centerInParent=&quot;true&quot;
  20. android:outlineSpotShadowColor=&quot;#2196F3&quot;
  21. android:text=&quot;The Random Game&quot;
  22. android:textColor=&quot;#006400&quot;
  23. android:textStyle=&quot;bold|italic&quot;
  24. app:layout_constraintEnd_toEndOf=&quot;parent&quot;
  25. app:layout_constraintStart_toStartOf=&quot;parent&quot;
  26. app:layout_constraintTop_toTopOf=&quot;parent&quot;
  27. /&gt;
  28. &lt;Button
  29. android:id=&quot;@+id/buttonstart&quot;
  30. android:layout_width=&quot;wrap_content&quot;
  31. android:layout_height=&quot;wrap_content&quot;
  32. android:layout_below=&quot;@id/game&quot;
  33. android:layout_centerInParent=&quot;true&quot;
  34. android:background=&quot;#8b0000&quot;
  35. android:text=&quot;Start&quot;
  36. android:textColor=&quot;#006400&quot;
  37. android:textStyle=&quot;bold|italic&quot;
  38. /&gt;
  39. &lt;TextView
  40. android:id=&quot;@+id/TVshowmessage&quot;
  41. android:layout_width=&quot;wrap_content&quot;
  42. android:layout_height=&quot;wrap_content&quot;
  43. android:layout_below=&quot;@id/buttonstart&quot;
  44. android:layout_centerInParent=&quot;true&quot;
  45. android:text=&quot;the number is&quot;
  46. android:textColor=&quot;#006400&quot;
  47. /&gt;
  48. &lt;Button
  49. android:id=&quot;@+id/buttonplus&quot;
  50. android:layout_width=&quot;wrap_content&quot;
  51. android:layout_height=&quot;wrap_content&quot;
  52. android:layout_below=&quot;@id/TVshowmessage&quot;
  53. android:layout_centerInParent=&quot;true&quot;
  54. android:background=&quot;#8b0000&quot;
  55. android:text=&quot;+&quot;
  56. android:textColor=&quot;#006400&quot;
  57. android:textSize=&quot;@android:dimen/app_icon_size&quot;
  58. android:textStyle=&quot;bold|italic&quot;
  59. /&gt;
  60. &lt;TextView
  61. android:id=&quot;@+id/TVcurrentnum&quot;
  62. android:layout_width=&quot;wrap_content&quot;
  63. android:layout_height=&quot;wrap_content&quot;
  64. android:layout_below=&quot;@id/buttonplus&quot;
  65. android:layout_centerInParent=&quot;true&quot;
  66. android:background=&quot;#8b0000&quot;
  67. android:text=&quot;0&quot;
  68. android:textColor=&quot;#006400&quot;
  69. /&gt;
  70. &lt;Button
  71. android:id=&quot;@+id/buttonminus&quot;
  72. android:layout_width=&quot;wrap_content&quot;
  73. android:layout_height=&quot;wrap_content&quot;
  74. android:layout_below=&quot;@id/TVcurrentnum&quot;
  75. android:layout_centerInParent=&quot;true&quot;
  76. android:background=&quot;#8b0000&quot;
  77. android:text=&quot;-&quot;
  78. android:textColor=&quot;#006400&quot;
  79. android:textSize=&quot;@android:dimen/app_icon_size&quot;
  80. /&gt;
  81. &lt;TextView
  82. android:id=&quot;@+id/tvcounter&quot;
  83. android:layout_width=&quot;wrap_content&quot;
  84. android:layout_height=&quot;66dp&quot;
  85. android:layout_below=&quot;@id/buttonrestart&quot;
  86. android:layout_centerInParent=&quot;true&quot;
  87. android:layout_marginBottom=&quot;300dp&quot;
  88. android:background=&quot;#8b0000&quot;
  89. android:text=&quot;counter&quot;
  90. android:textAlignment=&quot;center&quot;
  91. android:textColor=&quot;#006400&quot;
  92. android:textStyle=&quot;bold|italic&quot;
  93. /&gt;
  94. &lt;Button
  95. android:id=&quot;@+id/buttoncheck&quot;
  96. android:layout_width=&quot;wrap_content&quot;
  97. android:layout_height=&quot;wrap_content&quot;
  98. android:layout_below=&quot;@id/buttonminus&quot;
  99. android:layout_centerHorizontal=&quot;true&quot;
  100. android:layout_centerInParent=&quot;true&quot;
  101. android:background=&quot;#8b0000&quot;
  102. android:outlineSpotShadowColor=&quot;#008B02&quot;
  103. android:text=&quot;Check&quot;
  104. android:textColor=&quot;#006400&quot;
  105. android:textStyle=&quot;bold|italic&quot;
  106. /&gt;
  107. &lt;Button
  108. android:id=&quot;@+id/buttonrestart&quot;
  109. android:layout_width=&quot;wrap_content&quot;
  110. android:layout_height=&quot;wrap_content&quot;
  111. android:layout_below=&quot;@id/buttoncheck&quot;
  112. android:layout_centerInParent=&quot;true&quot;
  113. android:background=&quot;#8b0000&quot;
  114. android:text=&quot;restart&quot;
  115. android:textColor=&quot;#006400&quot;
  116. android:textStyle=&quot;bold|italic&quot;
  117. /&gt;
  118. &lt;/RelativeLayout&gt;
  119. &lt;!-- code from foundnm.xml--&gt;
  120. &lt;RelativeLayout
  121. android:id=&quot;@+id/grats&quot;
  122. android:layout_width=&quot;match_parent&quot;
  123. android:layout_height=&quot;match_parent&quot;
  124. android:visibility=&quot;gone&quot;
  125. &gt;
  126. &lt;TextView
  127. android:id=&quot;@+id/tvfound&quot;
  128. android:layout_width=&quot;wrap_content&quot;
  129. android:layout_height=&quot;wrap_content&quot;
  130. android:layout_above=&quot;@+id/buttonrestart1&quot;
  131. android:layout_centerInParent=&quot;true&quot;
  132. android:background=&quot;#8b0000&quot;
  133. android:text=&quot;congrats&quot;
  134. android:textColor=&quot;#006400&quot;
  135. android:textStyle=&quot;bold|italic&quot;
  136. /&gt;
  137. &lt;Button
  138. android:id=&quot;@+id/buttonrestart1&quot;
  139. android:layout_width=&quot;wrap_content&quot;
  140. android:layout_height=&quot;wrap_content&quot;
  141. android:layout_centerInParent=&quot;true&quot;
  142. android:background=&quot;#8b0000&quot;
  143. android:text=&quot;restart&quot;
  144. android:textColor=&quot;#006400&quot;
  145. android:textStyle=&quot;bold|italic&quot;
  146. /&gt;
  147. &lt;/RelativeLayout&gt;
  148. &lt;/RelativeLayout&gt;

Note that, when you run the app take note that some views are hidden, that is

  1. &lt;RelativeLayout&gt;
  2. &lt;!--page relative layout--&gt; This is visible (visibilty = visible )
  3. &lt;!--grats relative layout--&gt; This is invisible (visibility = gone)
  4. &lt;/RelativeLayout&gt;

After doing whatever its that you want to do(checking if number is in the counter.....), set visible to &lt;!--grats relative layout--&gt; and set visibility gone for &lt;!--page relative layout--&gt;

You need to modify the the if statement in this function: changepage(), to something like this

  1. public void changepage(){
  2. if (myC.ccheck() == &quot;equal&quot;){
  3. page.setVisibility(View.GONE);
  4. grats.setVisibility(View.VISIBLE);
  5. }
  6. else{
  7. grats.setVisibility(View.GONE);
  8. page.setVisibility(View.VISIBLE);
  9. }
  10. }

I hope you find this helpful. If you have issues, please comment, I will help you out.

huangapple
  • 本文由 发表于 2020年10月27日 19:25:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/64553434.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定