簡體   English   中英

如何使用主活動中的按鈕增加其他活動中的TextView值?

[英]How to increment a TextView value in a different activity with a button from the Main Activity?

好的,所以我嘗試過問這個問題,但是我沒有得到正確的答案,所以這次我將更加具體。

我有一個應用程序(可以自己玩的游戲),您可以在這里飲食,可以通過單擊按鈕創建關注者。 單擊“ 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?

單擊按鈕后,我不希望該按鈕將我重定向到活動。

您已經到了一半,您正在將Intent中的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
}

您必須以這種方式在活動內部證明textview

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

然后,在按鈕的onClick內可以設置數字

followerCountTextView.setText("" + followerCount);

我終於想通了!

我最終不得不使用以下代碼:

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個星期。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM