简体   繁体   English

如何更新android textview的文本

[英]How can I update text of android textview

I am creating a quiz game in android using java, Its first activity contains a TextView that represents the points earned ,displays the value stored in SharedPreference variable, This value is increasing if the user moves to another activities or playing game,But the textview in first activity still displays the initial value (It displays the final value only if i restart application), How can I solve this issue without using startActivityforresult 我正在使用Java在android中创建测验游戏,其第一个活动包含一个TextView,代表所赚取的积分,显示存储在SharedPreference变量中的值,如果用户移至其他活动或玩游戏,则该值会增加,但是textview在第一个活动仍显示初始值(仅当我重新启动应用程序时,它才显示最终值),如何在不使用startActivityforresult的情况下解决此问题

First activity code 首次活动代码

                int mode4 = Activity.MODE_PRIVATE;
                SharedPreferences data = getSharedPreferences("data", mode4);
                Integer coins = data.getInt("coins", 0);
                String coinsString = coins.toString();
                TextView coinView = (TextView) findViewById(R.id.coins);
                coinView.setText(coinsString);

Final activity code 最终活动代码

public void a_click(View ClickedButton)
{
    int mode=Activity.MODE_PRIVATE;
    SharedPreferences my=getSharedPreferences("alpha",mode);
    String z=my.getString("correct","data_loss");

    SharedPreferences data=getSharedPreferences("data",mode);
    Integer coins=data.getInt("coins", 0);
    if(z.compareTo("1")==0)
    {
        int mode66=Activity.MODE_PRIVATE;
        SharedPreferences my66=getSharedPreferences("session",mode66);
        int zedd=my66.getInt("qnum", 1);
        int coinsP=0;
        if(zedd==1)
        {
            coinsP=coins+5;
        }
        if(zedd==2)
        {
            coinsP=coins+5;
        }
        if(zedd==3)
        {
            coinsP=coins+10;
        }
        if(zedd==4)
        {
            coinsP=coins+15;
        }
        if(zedd==5)
        {
            coinsP=coins+20;
        }
        if(zedd==6)
        {
            coinsP=coins+25;
        }

        qnum++;
        mugu(qnum);
        SharedPreferences dataP=getSharedPreferences("data",mode);
        SharedPreferences.Editor e2=dataP.edit();
        e2.putInt("coins", coinsP);
        e2.commit();
    }

Override the resume method and set text in textview there as show below. 覆盖简历方法,并在textview中设置文本,如下所示。

@Override
public void onResume() {
    super.onResume();  // Always call the superclass method first
   textView.setText("YourText");
}

Calling startActivityforresult() is not a solution as it is not for this. 调用startActivityforresult()不是解决方案,因为它不是为此。 What you should do is simply update your UI in onResume() which will be called whenver your Activity goes to foreground (ie when user gets back from other activities) 您应该做的就是简单地在onResume()更新UI,无论何时您的Activity进入前台(即,当用户从其他活动中回来时onResume() ,都会调用该UI

@Override
public void onResume() {
   super.onResume();

   textView.setText(hiscore);
}
 public class MainProjectActivity extends Activity {
    /** Called when the activity is first created. */
   TextView displayScores;
    static int Score = 0;
   @Override
  protected void onResume(){
    super.onResume();
    // update your data here
        spSettings = getSharedPreferences(KEY,"VALUE");
        displayScores.setText(spSettings.getString("YOUR KEY",     
        "YOURVALUE")); 

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

        //Display Scores
        displayScores = (TextView)findViewById(R.id.scoreDisplay);
        displayScores.setText("Your Score : "+ Score);

        //Play Game button activity
        Button gameButton = (Button)findViewById(R.id.PlayButton);
        gameButton.setOnClickListener(new OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent play = new Intent(getApplicationContext(), com.sample.game.PlayScreen.class);
                startActivity(play);
            }
        });

put this code in your mainactivity 将此代码放入您的mainactivity中

Timer().scheduleAtFixedRate(new TimerTask() {
       public void run() {
                runOnUiThread(new Runnable() {
            public void run() {
                int mode = Activity.MODE_PRIVATE;
                SharedPreferences data = getSharedPreferences("data", mode);
                Integer coins = data.getInt("coins", 0);
                String coinsString = coins.toString();
                TextView coinView = (TextView) findViewById(R.id.coins);
                coinView.setText(coinsString);
            }
        });
        }
    }, 0, 500);

this code will update your textview for every 0.5 seconds 该代码将每0.5秒更新一次textview

As you are storing the data in Shared Preference so you can add listener for your preferences, as follows. 当您将数据存储在“共享首选项”中时,您可以如下添加首选项的侦听器。

   //Here you can register for changes in preference object
   preferences.registerOnSharedPreferenceChangeListener(listener);

Listener implementation will be as follows 侦听器的实现如下

SharedPreferences.OnSharedPreferenceChangeListener listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
        public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
            // Implementation
            tv.setText(prefs.getString(key, "defalut"));
        }
    };

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果TextView包含在活动中,如何更新TextView的文本? - How can I update the text of a TextView if the TextView is in an included activity? Android,如何在 OnClick 中从 TextView 获取文本 - Android, How can I get text from TextView in OnClick 如何在 function 中使用 textview Android Studio 在 Z93F725A074233FEC889ZDF48 中显示文本 - How can I display text in a function with textview Android Studio in java 如何使TextView绕过Android中的其他TextView? - How can I make a TextView to bypass other TextView in Android? 如何在TextView上侦听并在Android中对同一TextView进行更改 - How can I listen on TextView and make changes to the same TextView in android 如何在TextView中设置MaxLines但允许更新文本-Android - How to set MaxLines in TextView but allowing update text - Android Android - 如何在 TextView 中打印结果? - Android - How can I print the result in a TextView? 在Android中如何显示很长的文本(190个单词),有没有比textview更好的主意了? - In Android how can I show a very long text (190 words), is there any idea better then textview? 如何在android studio上的增强现实应用程序中在Textview中动态设置文本 - how can I dynamically set text in Textview in augmented reality application on the android studio Android:Textview。 如何在单个垂直边距中将文本左对齐? - Android: Textview. How can I align text to the left in single vertical margin?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM