如何使用MainActivity.class中的按钮更新不同XML文件中的TextView?

时间:2017-05-18 05:23:52

标签: java android textview

我想按下[创建新关注者]按钮,每按一次按钮,它会将 followers.xml 中的 textview 增加1。

这是我的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;
    public static TextView numberOfFollowers;


    //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);

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

        };//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
                case R.id.milestones:Intent intent = new Intent(MainActivity.this, milestones.class);
                MainActivity.this.startActivity(intent);
                break;

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

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

                //Followers Button Pressed
                case R.id.followers:intent = new Intent(MainActivity.this, followers.class);

                MainActivity.this.startActivity(intent);
                break;

                //Create Follower Button Pressed
                case R.id.createFollowerButton: 

                    numberOfFollowers = (TextView) numberOfFollowers;
                    FollowerCount++;

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

                    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 Follower
    //everytime button is clicked
    public final void AddFollower()
        {


        followerLimit++;
        FollowerCount++;

        }//End names()


    private class Follower
        {
            int influence;

            public Follower(int influence)
                {
                influence = 1;

                }

        }



    public void startWorshipping()
        {


        }
}//End MainActivity`

我遇到的问题是,每当按下主活动上的按钮时,我希望按钮将文本视图增加1。

2 个答案:

答案 0 :(得分:0)

将文本视图的宽度和高度设置为xml中的换行内容。 然后,对于每个按钮,单击增加textview的文本大小。

答案 1 :(得分:0)

您可以将值存储在MainActivtiy按钮上的shared Preferences中,在关注者屏幕中,您可以从共享首选项中获取值,并在textview中显示它。

<强>更新

中的

全球宣布

public static final String PREFS = "prefs";
public static final String COUNT = "count";
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
int count = 0;

然后在你的onCreate初始化

sharedPreferences = getSharedPreferences(PREFS, Context.MODE_PRIVATE);
editor = sharedPreferences.edit();

然后在按钮中单击

count++;
editor.putInt(COUNT,count);
editor.commit();

然后在您要显示值

的活动中
SharedPreferences sharedPreferences;
创建

sharedPreferences = getSharedPreferences(MainActivity.PREFS, Context.MODE_PRIVATE);
sharedPreferences.getInt(MainActivity.COUNT,0);

此get int值将返回点击次数