如何使用主活动中的按钮增加不同活动中的TextView值?

时间:2017-05-18 21:56:46

标签: java android button textview ide

好的,所以我之前尝试过问这个问题,但我没有得到正确的答案,所以这次我会更具体。

我有一个应用程序(自己玩的游戏),你可以在这里痴迷,只需点击一下按钮即可创建粉丝。点击Create Follower按钮后,我希望它将点击次数存储到名为followerCount的变量中。

这是MainActivity.class:

    package com.couchmango.godslife;

import android.app.*;
import android.content.*;
import android.os.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import java.util.*;
import android.preference.*;

public class MainActivity extends Activity implements OnClickListener
    {
        //Declare Constant Variables
        private Button createFollowerButton;
        private int followerLimit;
        private boolean reachedFollowerLimit;
        public static int followerCount;


        //Called when activity opens
        @Override
        protected void onCreate(Bundle savedInstanceState)
            {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            //Create Button
            createFollowerButton = (Button)findViewById(R.id.createFollowerButton);
            createFollowerButton.setOnClickListener(this);

            };//End onCreate

        @Override
        public void onRestoreInstanceState(Bundle savedInstanceState)
            {
            super.onRestoreInstanceState(savedInstanceState);
            // Restore UI state from the savedInstanceState.
            // This bundle has also been passed to onCreate.

            }


        @Override
        public void onClick(View v)
            {
            switch(v.getId()){

                    //Milestones Button Pressed
                    //Goes to Milestones activity
                    case R.id.milestones:Intent intent = new Intent(MainActivity.this, milestones.class);
                    MainActivity.this.startActivity(intent);
                    break;

                    //God Stats Button Pressed
                    //Goes to God Stats activity
                    case R.id.godStats:intent = new Intent(MainActivity.this, godStats.class);
                    MainActivity.this.startActivity(intent);
                    break;

                    //Influence Button Pressed
                    //Goes to Influence activity
                    case R.id.influence:intent = new Intent(MainActivity.this, influence.class);
                    MainActivity.this.startActivity(intent);
                    break;

                    //Followers Button Pressed
                    //Goes to Followers activity
                    case R.id.followers:intent = new Intent(MainActivity.this, followers.class);
                    intent = new Intent(MainActivity.this, followers.class);
                    intent.putExtra("FollowerCount", followerCount);
                    MainActivity.this.startActivity(intent);

                    break;

                    //Create Follower Button Pressed
                    //Increases followerCount & adds to followerLimit
                    //if followerLimit is not reached
                    case R.id.createFollowerButton: 

                        if(reachedFollowerLimit == false){AddFollower();}

                        //if followerLimit reached
                        //cannot add more followers
                        //(sets to true)
                        if(followerLimit == 10){

                        reachedFollowerLimit = true;

                        Context context = getApplicationContext();
                        CharSequence toastText = "Follower limit reached";
                        int duration = Toast.LENGTH_SHORT;

                        Toast toast = Toast.makeText(context, toastText, duration);
                        toast.show();

                        }//end if()
                    break;
                }//END SWITCH

            }//End OnCLICK





        //Adds to followerLimit/Increments followerCount
        //everytime button is clicked
        public final void AddFollower()
            {

            followerLimit++;
            followerCount++;

            }//End AddFollower

        /*Ignore this

        private class Follower
            {
                int influence;

                public Follower(int influence)
                    {
                    influence = 1;

                    }

            }



        public void startWorshipping()
            {


            }

        */


    }//End MainActivity

如果您需要查看布局,请参阅main.xml

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:background="#ECECEC">

    <LinearLayout
        android:layout_height="80dp"
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:gravity="center">

        <Button
            android:layout_height="match_parent"
            android:layout_width="wrap_content"
            android:text="Milestones"
            android:background="#FFFFFF"
            android:textSize="10sp"
            android:singleLine="true"
            android:id="@+id/milestones"
            android:onClick="onClick"/>

        <View
            android:background="?android:attr/dividerHorizontal"
            android:layout_height="match_parent"
            android:layout_width=".5dp"/>

        <Button
            android:layout_height="match_parent"
            android:layout_width="wrap_content"
            android:text="God Stats"
            android:background="#FFFFFF"
            android:textSize="10sp"
            android:singleLine="true"
            android:id="@+id/godStats"
            android:onClick="onClick"/>

        <View
            android:background="?android:attr/dividerHorizontal"
            android:layout_height="match_parent"
            android:layout_width=".5dp"/>

        <Button
            android:layout_height="match_parent"
            android:layout_width="wrap_content"
            android:text="Influence Stats"
            android:background="#FFFFFF"
            android:textSize="10sp"
            android:singleLine="false"
            android:padding="3dp"
            android:lines="2"
            android:minLines="2"
            android:textAlignment="center"
            android:id="@+id/influence"
            android:onClick="onClick"/>

        <View
            android:background="?android:attr/dividerHorizontal"
            android:layout_height="match_parent"
            android:layout_width=".5dp"/>

        <Button
            android:layout_height="match_parent"
            android:layout_width="wrap_content"
            android:text="Followers"
            android:background="#FFFFFF"
            android:gravity="center"
            android:textSize="10sp"
            android:singleLine="true"
            android:id="@+id/followers"
            android:onClick="onClick"/>

    </LinearLayout>

    <View
        android:background="?android:attr/dividerVertical"
        android:layout_height="4dp"
        android:layout_width="match_parent"/>

    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:orientation="horizontal">

        <TextView
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:layout_width="wrap_content"
            android:text="Follower Feed"
            android:padding="4dp"/>

        <Button
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:text="Create Follower"
            android:layout_marginLeft="36dp"
            android:layout_marginRight="5dp"
            android:textSize="10sp"
            android:id="@+id/createFollowerButton"
            android:onClick="onClick"/>

    </LinearLayout>

    <View
        android:background="?android:attr/dividerVertical"
        android:layout_height="1dp"
        android:layout_width="match_parent"/>

    <TextView
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_width="match_parent"
        android:text="Create a new follower"
        android:paddingLeft="5dp"
        android:id="@+id/textview1"
        android:paddingTop="6dp"/>

</LinearLayout>

followerCount变量将显示在我的followers.xml文件中的TextView中,如下所示:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:orientation="horizontal">

        <TextView
            android:layout_height="60dp"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="wrap_content"
            android:text="Follower Count"
            android:gravity="left|center"
            android:textSize="30sp"
            android:padding="10dp"/>

        <View
            android:background="?android:attr/dividerHorizontal"
            android:layout_height="match_parent"
            android:layout_width="2dp"/>

        <TextView
            android:layout_height="match_parent"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="match_parent"
            android:text="0"
            android:gravity="center_vertical|center_horizontal"
            android:textSize="30sp"
            android:id="@+id/followerCount"/>

    </LinearLayout>

</LinearLayout>

这里是followers.xml的Java文件

package com.couchmango.godslife;

import android.annotation.*;
import android.app.*;
import android.content.*;
import android.os.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;

public class followers extends Activity implements OnClickListener
    {
        @Override
        protected void onCreate(Bundle savedInstanceState)
            {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.followers);




            };//End onCreate


        //Saves screen results when back button pressed
        @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
        @Override
        public Intent getParentActivityIntent()
            {
            return super.getParentActivityIntent().addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            }



        @Override
        public void onClick(View p1)
            {


            // TODO: Implement this method
            }
    }

我的问题是:每次点击MainActivity中的Create Followers按钮时,如何使followers.xml中的T​​extView增加1?

点击按钮后,我不希望按钮将我重定向到活动。

3 个答案:

答案 0 :(得分:0)

你已经到了一半,你在跟随者活动的意图中发送了followerCount int(这不应该是静态的)作为额外的。

在关注者活动中,在onCreate方法中,您需要编写如下内容:

Bundle extras = getIntent().getExtras();
    if (extras == null) {
    return;
}
int followerCount = extras.getInt("FollowerCount"); 
    // assign followerCount to a member variable in your followers activity
}

答案 1 :(得分:0)

您必须以这种方式在活动中反映textview

TextView followerCountTextView = (TextView)findViewById(R.id.followerCount);

然后,在按钮的onClick内你可以设置数字

followerCountTextView.setText("" + followerCount);

答案 2 :(得分:0)

我终于明白了!

我最终不得不使用此代码:

public static boolean buttonIsClicked;
public static Bundle bundle = new Bundle();

这就是按钮点击的内容

@Override
    public void onClick(View v)
        {
        Intent i=new Intent(MainActivity.this, followers.class);
        switch(v.getId()){

                //Milestones Button Pressed
                //Goes to Milestones activity
                case R.id.milestones:Intent intent = new Intent(MainActivity.this, milestones.class);
                MainActivity.this.startActivity(intent);
                break;

                //God Stats Button Pressed
                //Goes to God Stats activity
                case R.id.godStats:intent = new Intent(MainActivity.this, godStats.class);
                MainActivity.this.startActivity(intent);
                break;

                //Influence Button Pressed
                //Goes to Influence activity
                case R.id.influence:intent = new Intent(MainActivity.this, influence.class);
                MainActivity.this.startActivity(intent);
                break;

                //Followers Button Pressed
                //Goes to Followers activity
                case R.id.followers:

                followerCount = (TextView)findViewById(R.id.followerCount);

                bundle.putInt("followers",followerCountInt); //This is for an int
                i.putExtras(bundle);
                startActivity(i);
                break;

                //Create Follower Button Pressed
                //Increases followerCount & adds to followerLimit
                //if followerLimit is not reached
                case R.id.createFollowerButton: 

                followerCount = (TextView)findViewById(R.id.followerCount);
                bundle = new Bundle();
                bundle.putInt("followers",followerCountInt); //This is for an int
                i.putExtras(bundle);


                    if(reachedFollowerLimit == false){AddFollower();}

                    //if followerLimit reached
                    //cannot add more followers
                    //(sets to true)
                    if(followerLimit == 10){


                    reachedFollowerLimit = true;

                    Context context = getApplicationContext();
                    CharSequence toastText = "Follower limit reached";
                    int duration = Toast.LENGTH_SHORT;

                    Toast toast = Toast.makeText(context, toastText, duration);
                    toast.show();

                    }//end if()
                break;
            }//END SWITCH

        }//End OnCLICK

这是我对followers.class文件的原因

    package com.couchmango.godslife;

import android.annotation.*;
import android.app.*;
import android.content.*;
import android.os.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;

public class followers extends Activity implements OnClickListener
    {
        TextView followerCount;

        @Override
        protected void onCreate(Bundle savedInstanceState)
            {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.followers);

            followerCount = (TextView)findViewById(R.id.followerCount);

            if(MainActivity.buttonIsClicked==false)
            {
            Bundle bundle = null;
            bundle = this.getIntent().getExtras();
            int myInt = bundle.getInt("followers");//this is for an int
            followerCount.setText(String.valueOf(myInt));
            }

            };//End onCreate

我设置此方法,基本上单击Create Follower,它运行的意图增加了textview,但实际上打开 followers.java活动。

但与此同时,如果您点击Followers按钮,会显示followers.xml文件,而不会增加任何内容。

这正是我想要做的事情,我终于在玩了一些提供的代码后想出来了!

谢谢你们!被困在这个约2周。