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

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

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

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

FoundNum layout xml - screen 2

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

MainActivity Java

package com.example.findrandomnumber;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button buttonstart, buttonplus, buttonminus, buttoncheck, buttonrestart, buttonrestart1;
    private TextView TVcurrentnum, TVshowmessage, tvcounter, tvfoundnum;
    private String TAG = "gilog";
    private Controllerguessnum myC;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // ... Java code ...
    }

    // ... Other methods ...

    public void changepage() {
        // ... Java code ...
    }

    @Override
    public void onClick(View v) {
        // ... Java code ...
    }
}

Controllerguessnum Java

package com.example.findrandomnumber;

public class Controllerguessnum {
    // ... Java code ...
}

Modelguessnum Java

package com.example.findrandomnumber;

import java.util.Random;

public class Modelguessnum {
    // ... Java code ...
}

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

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;RelativeLayout
   xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
   xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
   xmlns:tools=&quot;http://schemas.android.com/tools&quot;
   android:layout_width=&quot;match_parent&quot;
   android:layout_height=&quot;match_parent&quot;
   tools:context=&quot;.MainActivity&quot;
   android:background=&quot;#00008b&quot;
   android:id=&quot;@+id/page&quot;


   &gt;


   &lt;TextView
       android:id=&quot;@+id/game&quot;
       android:layout_width=&quot;wrap_content&quot;
       android:layout_height=&quot;wrap_content&quot;
       android:text=&quot;The Random Game&quot;
       android:textStyle=&quot;bold|italic&quot;
       android:layout_centerInParent=&quot;true&quot;
       android:layout_alignParentTop=&quot;true&quot;
       app:layout_constraintEnd_toEndOf=&quot;parent&quot;
       app:layout_constraintStart_toStartOf=&quot;parent&quot;
       android:textColor=&quot;#006400&quot;
       android:outlineSpotShadowColor=&quot;#2196F3&quot;
       app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
   &lt;Button
       android:id=&quot;@+id/buttonstart&quot;
       android:text=&quot;Start&quot;
       android:textStyle=&quot;bold|italic&quot;
       android:layout_width=&quot;wrap_content&quot;
       android:layout_height=&quot;wrap_content&quot;
       android:layout_below=&quot;@id/game&quot;
       android:layout_centerInParent=&quot;true&quot;
       android:textColor=&quot;#006400&quot;
       android:background=&quot;#8b0000&quot;

       /&gt;
   &lt;TextView
       android:id=&quot;@+id/TVshowmessage&quot;
       android:text=&quot;the number is&quot;
       android:layout_width=&quot;wrap_content&quot;
       android:layout_height=&quot;wrap_content&quot;
       android:layout_below=&quot;@id/buttonstart&quot;
       android:layout_centerInParent=&quot;true&quot;
       android:textColor=&quot;#006400&quot;


       /&gt;
   &lt;Button
       android:id=&quot;@+id/buttonplus&quot;
       android:text=&quot;+&quot;
       android:textStyle=&quot;bold|italic&quot;
       android:textSize=&quot;@android:dimen/app_icon_size&quot;
       android:layout_width=&quot;wrap_content&quot;
       android:layout_height=&quot;wrap_content&quot;
       android:layout_below=&quot;@id/TVshowmessage&quot;
       android:layout_centerInParent=&quot;true&quot;
       android:textColor=&quot;#006400&quot;
       android:background=&quot;#8b0000&quot;
       /&gt;
   &lt;TextView
       android:id=&quot;@+id/TVcurrentnum&quot;
       android:text=&quot;0&quot;
       android:layout_width=&quot;wrap_content&quot;
       android:layout_height=&quot;wrap_content&quot;
       android:layout_below=&quot;@id/buttonplus&quot;
       android:layout_centerInParent=&quot;true&quot;
       android:textColor=&quot;#006400&quot;
       android:background=&quot;#8b0000&quot;

       /&gt;
   &lt;Button
       android:id=&quot;@+id/buttonminus&quot;
       android:text=&quot;-&quot;
       android:textSize=&quot;@android:dimen/app_icon_size&quot;
       android:layout_width=&quot;wrap_content&quot;
       android:layout_height=&quot;wrap_content&quot;
       android:layout_below=&quot;@id/TVcurrentnum&quot;
       android:layout_centerInParent=&quot;true&quot;
       android:textColor=&quot;#006400&quot;
       android:background=&quot;#8b0000&quot;

       /&gt;

   &lt;TextView
       android:id=&quot;@+id/tvcounter&quot;
       android:text=&quot;counter&quot;
       android:layout_width=&quot;wrap_content&quot;
       android:layout_height=&quot;66dp&quot;
       android:layout_centerInParent=&quot;true&quot;
       android:layout_marginBottom=&quot;300dp&quot;
       android:background=&quot;#8b0000&quot;
       android:textColor=&quot;#006400&quot;
       android:textStyle=&quot;bold|italic&quot;
       android:textAlignment=&quot;center&quot;
       android:layout_below=&quot;@id/buttonrestart&quot;
       /&gt;

   &lt;Button
       android:id=&quot;@+id/buttoncheck&quot;
       android:layout_width=&quot;wrap_content&quot;
       android:layout_height=&quot;wrap_content&quot;
       android:layout_centerInParent=&quot;true&quot;
       android:layout_centerHorizontal=&quot;true&quot;
       android:textColor=&quot;#006400&quot;
       android:background=&quot;#8b0000&quot;
       android:textStyle=&quot;bold|italic&quot;
       android:outlineSpotShadowColor=&quot;#008B02&quot;
       android:text=&quot;Check&quot;
       android:layout_below=&quot;@id/buttonminus&quot;
       /&gt;
   &lt;Button
       android:id=&quot;@+id/buttonrestart&quot;
       android:text=&quot;restart&quot;
       android:layout_width=&quot;wrap_content&quot;
       android:layout_height=&quot;wrap_content&quot;
       android:layout_centerInParent=&quot;true&quot;
       android:textColor=&quot;#006400&quot;
       android:background=&quot;#8b0000&quot;
       android:textStyle=&quot;bold|italic&quot;
       android:layout_below=&quot;@id/buttoncheck&quot;

       /&gt;

&lt;/RelativeLayout&gt; 

foundnum xml screen 2

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;RelativeLayout
    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:background=&quot;#00008b&quot;
    android:id=&quot;@+id/grats&quot;
    &gt;

    &lt;TextView
        android:id=&quot;@+id/tvfound&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:text=&quot;congrats&quot;
        android:textColor=&quot;#006400&quot;
        android:background=&quot;#8b0000&quot;
        android:textStyle=&quot;bold|italic&quot;
        android:layout_above=&quot;@+id/buttonrestart1&quot;
        android:layout_centerInParent=&quot;true&quot;
        /&gt;


    &lt;Button
        android:id=&quot;@+id/buttonrestart1&quot;
        android:text=&quot;restart&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_centerInParent=&quot;true&quot;
        android:textColor=&quot;#006400&quot;
        android:background=&quot;#8b0000&quot;
        android:textStyle=&quot;bold|italic&quot;

        /&gt;

&lt;/RelativeLayout&gt;

main activity java.

    package com.example.findrandomnumber;

    import androidx.appcompat.app.AppCompatActivity;

    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.RelativeLayout;
    import android.widget.TextView;
    import android.widget.Toast;


     public class MainActivity extends AppCompatActivity implements View.OnClickListener {
        private Button buttonstart, buttonplus, buttonminus, buttoncheck, buttonrestart,buttonrestart1;
        private TextView TVcurrentnum, TVshowmessage, tvcounter,tvfoundnum;
        private String TAG = &quot;gilog&quot;,currentnum,counter;
        private Controllerguessnum myC;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, &quot;onCreate: hi&quot;);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        buttonstart =(Button) findViewById(R.id.buttonstart);
        buttonplus = (Button)findViewById(R.id.buttonplus);
        buttonminus =(Button) findViewById(R.id.buttonminus);
        buttoncheck = (Button)findViewById(R.id.buttoncheck);
        buttonrestart = (Button)findViewById(R.id.buttonrestart);
        buttonrestart1 = (Button)findViewById(R.id.buttonrestart1);

        TextView  tvcounter= findViewById(R.id.tvcounter);
        TextView  TVshowmessage= findViewById(R.id.TVshowmessage);
        TextView  TVcurrentnum= findViewById(R.id.TVcurrentnum);
        TextView tvfoundnum= findViewById(R.id.tvfound);
        findViewById(R.id.page);
        findViewById(R.id.grats);


        buttonstart.setOnClickListener(this);
        buttonrestart.setOnClickListener(this);
        buttoncheck.setOnClickListener(this);
        buttonplus.setOnClickListener(this);
        buttonminus.setOnClickListener(this);

        Log.d(TAG, &quot;onCreate: set clickers worked&quot;);


    }

    public void startGame() {
            myC = new Controllerguessnum();

           Log.d(TAG, &quot;startGame: random number is &quot; + myC.crandom());


    }
    public void showmessage(){
        TextView  TVshowmessage= findViewById(R.id.TVshowmessage);

        Log.d(TAG, &quot;onClick: button check was pressed&quot;);


        if (myC.ccheck() == &quot;smaller&quot;){
            Log.d(TAG, &quot;onClick: check is ? &quot;+ myC.ccheck() +&quot; numbers: &quot;+myC.ccurrentnum()+ &quot;&lt;&quot;+ myC.crandom());
            TVshowmessage.setText(&quot;number is smaller than random number&quot; + 0);
        }

        else
            if (myC.ccheck() == &quot;equal&quot;){
                Log.d(TAG, &quot;onClick: check is ? &quot;+ myC.ccheck() + &quot;=&quot;+ myC.crandom());
                TVshowmessage.setText(&quot;congrats you found the number, &quot;+String.valueOf(myC.ccounter()/2)+&quot; tries,&quot;+ &quot; press restart to play again&quot; );
            }
        else {
            if(myC.ccheck() == &quot;bigger&quot;) {
                Log.d(TAG, &quot;onClick: check is ? &quot;+ myC.ccheck() + &quot;&gt;&quot;+ myC.crandom());
                TVshowmessage.setText(&quot;number you chose is bigger than random number&quot; + 0);
            }
        }

    }
    public void changepage(){
        TextView tvfoundnum= findViewById(R.id.tvfound);
        RelativeLayout grats= findViewById(R.id.grats);
        RelativeLayout page= findViewById(R.id.page);
        TextView  TVshowmessage= findViewById(R.id.TVshowmessage);
        if (myC.ccheck() == &quot;equal&quot;){
            page.setVisibility(View.GONE);
            setContentView(grats);
        }
        else{
            setContentView(page);

        }
    }


    @Override
    public void onClick(View v) {
        buttonstart =(Button) findViewById(R.id.buttonstart);
        buttonplus = (Button)findViewById(R.id.buttonplus);
        buttonminus =(Button) findViewById(R.id.buttonminus);
        buttoncheck = (Button)findViewById(R.id.buttoncheck);
        buttonrestart = (Button)findViewById(R.id.buttonrestart);
        TVcurrentnum= findViewById(R.id.TVcurrentnum);
        tvcounter= findViewById(R.id.tvcounter);


        if (v.getId() == R.id.buttonstart || v.getId() == R.id.buttonrestart) {
           startGame();
        }
        if (v.getId() == R.id.buttonrestart1){
            findViewById(R.id.page).setVisibility(View.VISIBLE);
            findViewById(R.id.grats).setVisibility(View.GONE);
            startGame();
        }
        //button check
        if (v.getId() == R.id.buttoncheck) {
            showmessage();
            myC.ccntplus();
            tvcounter.setText(String.valueOf((int)(myC.ccounter())+ 0));
            Log.d(TAG, &quot;onClick: counter returned&quot;+ tvcounter.getText());
            changepage();
        }
        if (v.getId() == R.id.buttonplus) {
            Log.d(TAG, &quot;onClick: button plus was pressed&quot;);
            myC.cplus();
            TVcurrentnum.setText(String.valueOf((int)(myC.ccurrentnum()) + 0));

        } else if (v.getId() == R.id.buttonminus) {
            Log.d(TAG, &quot;onClick: button minus was pressed&quot;);
            myC.cminus();
            TVcurrentnum.setText(String.valueOf((int)(myC.ccurrentnum()) + 0));


        }
    }
}

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


package com.example.findrandomnumber;

public class Controllerguessnum {

    private Modelguessnum myModel1;
    public double counter=0;
    private int random;

    public Controllerguessnum(){
        myModel1= new Modelguessnum();
        cstartGame();
        this.counter=0;
    }
    public void cstartGame(){
        myModel1.mStartGame();
        this.counter=0;    }
    public void crestartgame(){
        myModel1.mStartGame();
        this.counter=0;
    }
    public void cplus(){
        myModel1.mplus();
    }
    public void cminus(){
        myModel1.mminus();
    }
    public void ccntplus(){
        this.counter++;
    }

    public String ccheck(){

        String res = myModel1.mcheck();
        return res;
    }

    public double ccurrentnum(){
        int cnum=myModel1.getCurrentnum();
        return cnum;

    }

    public double ccounter(){
        double cnt= this.counter;
        return cnt;
}
    public int crandom(){
        this.random = myModel1.getRandomnum();
        return this.random;
}
}

model.java

package com.example.findrandomnumber;

import java.util.Random;

public class Modelguessnum {
    private int currentnum;
    private int  randomnum;
    private  int counter=0;

    public Modelguessnum() {

    }
    public void mStartGame(){
//        this.randomnum= new Random().nextInt( 100);
//        this.currentnum= new Random().nextInt(100);
        this.randomnum=5;
        this.currentnum=5;
    }
    public int getCurrentnum(){
        int mnum= currentnum;
        return mnum;
    }
    public int getRandomnum(){
        return randomnum;
    }


    public void mplus(){
        this.currentnum++;
    }
    public void mminus()
    {
        this.currentnum--;
    }

    public String mcheck() {
        //-1 is smaller
        //0 equal
        //1 is bigger
        if (this.currentnum &gt; this.randomnum) {
            return &quot;bigger&quot;;
        }
        else
          if (this.currentnum==this.randomnum){
               return &quot;equal&quot;;
        }

        else
            if(this.currentnum &lt; this.randomnum){
                return &quot;smaller&quot;;
            }
        else
           return &quot;neither&quot;;
}
}

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:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.findrandomnumber, PID: 480
    java.lang.IllegalArgumentException: Cannot add a null child view to a ViewGroup
        at android.view.ViewGroup.addView(ViewGroup.java:3718)
        at android.view.ViewGroup.addView(ViewGroup.java:3700)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:687)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:175)
        at com.example.findrandomnumber.MainActivity.changepage(MainActivity.java:95)
        at com.example.findrandomnumber.MainActivity.onClick(MainActivity.java:129)
        at android.view.View.performClick(View.java:4780)
        at android.view.View$PerformClick.run(View.java:19866)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        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

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

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

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

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

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

public void changepage(){
    if (myC.ccheck().equals("equal")){
        page.setVisibility(View.GONE);
        grats.setVisibility(View.VISIBLE);
    } else {
        grats.setVisibility(View.GONE);
        page.setVisibility(View.VISIBLE);
    }
}

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

英文:

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.

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    tools:context=&quot;.MainActivity&quot;
    &gt;

  &lt;RelativeLayout
      android:id=&quot;@+id/page&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      &gt;

    &lt;TextView
        android:id=&quot;@+id/game&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_alignParentTop=&quot;true&quot;
        android:layout_centerInParent=&quot;true&quot;
        android:outlineSpotShadowColor=&quot;#2196F3&quot;
        android:text=&quot;The Random Game&quot;
        android:textColor=&quot;#006400&quot;
        android:textStyle=&quot;bold|italic&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot;
        /&gt;
    &lt;Button
        android:id=&quot;@+id/buttonstart&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_below=&quot;@id/game&quot;
        android:layout_centerInParent=&quot;true&quot;
        android:background=&quot;#8b0000&quot;
        android:text=&quot;Start&quot;
        android:textColor=&quot;#006400&quot;
        android:textStyle=&quot;bold|italic&quot;

        /&gt;
    &lt;TextView
        android:id=&quot;@+id/TVshowmessage&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_below=&quot;@id/buttonstart&quot;
        android:layout_centerInParent=&quot;true&quot;
        android:text=&quot;the number is&quot;
        android:textColor=&quot;#006400&quot;


        /&gt;
    &lt;Button
        android:id=&quot;@+id/buttonplus&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_below=&quot;@id/TVshowmessage&quot;
        android:layout_centerInParent=&quot;true&quot;
        android:background=&quot;#8b0000&quot;
        android:text=&quot;+&quot;
        android:textColor=&quot;#006400&quot;
        android:textSize=&quot;@android:dimen/app_icon_size&quot;
        android:textStyle=&quot;bold|italic&quot;
        /&gt;
    &lt;TextView
        android:id=&quot;@+id/TVcurrentnum&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_below=&quot;@id/buttonplus&quot;
        android:layout_centerInParent=&quot;true&quot;
        android:background=&quot;#8b0000&quot;
        android:text=&quot;0&quot;
        android:textColor=&quot;#006400&quot;

        /&gt;
    &lt;Button
        android:id=&quot;@+id/buttonminus&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_below=&quot;@id/TVcurrentnum&quot;
        android:layout_centerInParent=&quot;true&quot;
        android:background=&quot;#8b0000&quot;
        android:text=&quot;-&quot;
        android:textColor=&quot;#006400&quot;
        android:textSize=&quot;@android:dimen/app_icon_size&quot;

        /&gt;

    &lt;TextView
        android:id=&quot;@+id/tvcounter&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;66dp&quot;
        android:layout_below=&quot;@id/buttonrestart&quot;
        android:layout_centerInParent=&quot;true&quot;
        android:layout_marginBottom=&quot;300dp&quot;
        android:background=&quot;#8b0000&quot;
        android:text=&quot;counter&quot;
        android:textAlignment=&quot;center&quot;
        android:textColor=&quot;#006400&quot;
        android:textStyle=&quot;bold|italic&quot;
        /&gt;

    &lt;Button
        android:id=&quot;@+id/buttoncheck&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_below=&quot;@id/buttonminus&quot;
        android:layout_centerHorizontal=&quot;true&quot;
        android:layout_centerInParent=&quot;true&quot;
        android:background=&quot;#8b0000&quot;
        android:outlineSpotShadowColor=&quot;#008B02&quot;
        android:text=&quot;Check&quot;
        android:textColor=&quot;#006400&quot;
        android:textStyle=&quot;bold|italic&quot;
        /&gt;
    &lt;Button
        android:id=&quot;@+id/buttonrestart&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_below=&quot;@id/buttoncheck&quot;
        android:layout_centerInParent=&quot;true&quot;
        android:background=&quot;#8b0000&quot;
        android:text=&quot;restart&quot;
        android:textColor=&quot;#006400&quot;
        android:textStyle=&quot;bold|italic&quot;

        /&gt;

  &lt;/RelativeLayout&gt;

  &lt;!-- code from foundnm.xml--&gt;

  &lt;RelativeLayout
      android:id=&quot;@+id/grats&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      android:visibility=&quot;gone&quot;
      &gt;

    &lt;TextView
        android:id=&quot;@+id/tvfound&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_above=&quot;@+id/buttonrestart1&quot;
        android:layout_centerInParent=&quot;true&quot;
        android:background=&quot;#8b0000&quot;
        android:text=&quot;congrats&quot;
        android:textColor=&quot;#006400&quot;
        android:textStyle=&quot;bold|italic&quot;
        /&gt;


    &lt;Button
        android:id=&quot;@+id/buttonrestart1&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_centerInParent=&quot;true&quot;
        android:background=&quot;#8b0000&quot;
        android:text=&quot;restart&quot;
        android:textColor=&quot;#006400&quot;
        android:textStyle=&quot;bold|italic&quot;

        /&gt;

  &lt;/RelativeLayout&gt;

&lt;/RelativeLayout&gt; 

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

&lt;RelativeLayout&gt;

   &lt;!--page relative layout--&gt;  This is visible (visibilty = visible )
   &lt;!--grats relative layout--&gt;  This is invisible (visibility = gone)
    
&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

public void changepage(){

        if (myC.ccheck() == &quot;equal&quot;){
            page.setVisibility(View.GONE);
            grats.setVisibility(View.VISIBLE);
        }
        else{
            grats.setVisibility(View.GONE);
            page.setVisibility(View.VISIBLE);

        }
    }

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:

确定