简体   繁体   English

TextView未在Android中更新

[英]TextView isn't updating in Android

I'm making a simple "novelty" app that counts how many sneezes are done, it's more of a fun app that builds up experience until I do my huge project during the summer. 我正在制作一个简单的“新奇”应用程序,它计算了打喷嚏的次数,它更像是一个有趣的应用程序,可以积累经验,直到我在夏天做我的大型项目。 I have two buttons, one adds a sneeze and the other clears how many sneezes there currently are. 我有两个按钮,一个添加喷嚏,另一个清除当前打喷嚏的次数。 It holds the highest number of sneezes that there were previously. 它拥有以前最多的打喷嚏。 The problem is, the TextViews never update, they only initialize to zero. 问题是,TextViews永远不会更新,它们只会初始化为零。 I used a Toast.makeText() to make sure that the buttons are working (they are). 我使用Toast.makeText()来确保按钮正常工作(它们是)。 Any help is appreciated. 任何帮助表示赞赏。 Thanks 谢谢

Java code: Java代码:

public class MainActivity extends ActionBarActivity {

   private int record_number = 0;
   private int current_number = 0;

   protected void onCreate(Bundle savedInstanceState)
   {

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


        Button add_one = (Button) findViewById(R.id.addone);
        Button clear_1 = (Button) findViewById(R.id.clear);

        add_one.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v)
            {
                current_number += 1;
                Toast.makeText(MainActivity.this, "Button Clicked " + current_number, Toast.LENGTH_SHORT).show();
            }
        });

        clear_1.setOnClickListener(new OnClickListener() {
            public void onClick(View v)
            {
                 current_number = 0;
                 Toast.makeText(MainActivity.this, "Button Clicked", Toast.LENGTH_SHORT).show();
            }
        });

        TextView rec_text = (TextView) findViewById(R.id.record_num);
        TextView cur_text = (TextView) findViewById(R.id.current_num);


        if (current_number >= record_number)
        {
             record_number = current_number;
        }

        rec_text.setText(String.valueOf(record_number));
        cur_text.setText(String.valueOf(current_number));

    }
 }

XML: XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.michail.sneezecounter.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Sneeze Counter"
        android:id="@+id/title"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Record Number of Sneezes:"
        android:id="@+id/textView"
        android:layout_below="@+id/title"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="72dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add one Sneeze"
        android:id="@+id/addone"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="76dp"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Clear Current Number of Sneezes"
        android:id="@+id/clear"
        android:layout_marginBottom="13dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:id="@+id/record_num"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true"
        android:singleLine="false" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Current Number of Sneezes:"
        android:id="@+id/currentLabel"
        android:layout_centerVertical="true"
        android:layout_alignRight="@+id/textView"
        android:layout_alignEnd="@+id/textView" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_below="@+id/currentLabel"
        android:layout_alignLeft="@+id/record_num"
        android:layout_alignStart="@+id/record_num"
        android:layout_marginTop="21dp"
        android:id="@+id/current_num" />

</RelativeLayout>

You need to update the text of the TextViews inside the onClickListeners. 您需要更新onClickListeners中的TextViews文本。 In fact, all your logic for counting, clearing, and recording the record needs to be done in your onClickListeners (or methods called by them). 事实上,计算,清除和记录记录的所有逻辑都需要在onClickListeners(或它们调用的方法)中完成。 Right now you only do it once in onCreate, then never again. 现在你只在onCreate做一次,然后再也不做。 You can do this in onCreate: 您可以在onCreate中执行此操作:

final TextView cur_text = (TextView) findViewById(R.id.current_num);
add_one.setOnClickListener(new OnClickListener() { 
    public void onClick(View v) { 
        current_number += 1;
        cur_text.setText(Integer.toString(current_number);
    } 
});

And similar for the other TextView & onClickListener. 和其他TextView和onClickListener类似。

You only set the contents of your TextView s once. 您只需设置TextView的内容一次。 You should update them every time you get a click event. 每次收到点击事件时都应该更新它们。 Specifically: 特别:

add_one.setOnClickListener(new OnClickListener()
{
    public void onClick(View v)
    {
        current_number += 1;

        if (current_number >= record_number)
        {
            record_number = current_number;
            rec_text.setText(String.valueOf(record_number));
        }

        cur_text.setText(String.valueOf(current_number));
    }
});

clear_1.setOnClickListener(new OnClickListener() {
    public void onClick(View v)
    {
         current_number = 0;
         cur_text.setText(String.valueOf(current_number));
    }
});

NOTE: if your variables current_number, record_number, cur_text, and rec_text aren't already declared as class member variables, you'll want to do that do that so that they're accessible once you leave the scope of the method you're doing all this in (I assume it's onCreate(...) . 注意:如果您的变量current_number,record_number,cur_text和rec_text尚未声明为类成员变量,那么您需要这样做,以便在您离开正在执行的方法的范围后可以访问它们所有这一切(我假设它是onCreate(...)

What you are going to need to do here is update the labels during the on click events of the button. 您需要在此处执行的操作是在按钮的单击事件期间更新标签。 You currently only update them on activity create. 您目前只在活动创建时更新它们。 This doesn't execute every time there is a click. 每次点击都不会执行此操作。 Can I answer any questions about the fixed up version below? 我可以回答有关下面修复版本的任何问题吗?

   protected void onCreate(Bundle savedInstanceState)
   {

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

        final TextView rec_text = (TextView) findViewById(R.id.record_num);
        final TextView cur_text = (TextView) findViewById(R.id.current_num);
        Button add_one = (Button) findViewById(R.id.addone);
        Button clear_1 = (Button) findViewById(R.id.clear);

        add_one.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v)
            {
                current_number += 1;

                if (current_number >= record_number)
                     record_number = current_number;

                cur_text.setText(String.valueOf(current_number));
                rec_text.setText(String.valueOf(record_number));
            }
        });

        clear_1.setOnClickListener(new OnClickListener() {
            public void onClick(View v)
            {
                 current_number = 0;
                 cur_text.setText(String.valueOf(current_number));
                 rec_text.setText(String.valueOf(record_number));
            }
        });

        cur_text.setText(String.valueOf(current_number));
        rec_text.setText(String.valueOf(record_number));
    }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM