TextView在Android中无法更新

时间:2014-05-11 05:20:27

标签: java android

我正在制作一个简单的“新奇”应用程序,它计算了打喷嚏的次数,它更像是一个有趣的应用程序,可以积累经验,直到我在夏天完成我的大型项目。我有两个按钮,一个添加喷嚏,另一个清除当前打喷嚏的次数。它拥有以前最多的打喷嚏。问题是,TextViews永远不会更新,它们只会初始化为零。我使用Toast.makeText()来确保按钮正常工作(它们是)。任何帮助表示赞赏。感谢

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:

<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>

3 个答案:

答案 0 :(得分:1)

您需要更新onClickListeners中TextView的文本。事实上,计算,清除和记录记录的所有逻辑都需要在onClickListeners(或它们调用的方法)中完成。现在你只在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);
    } 
});

与其他TextView&amp;相似onClickListener。

答案 1 :(得分:1)

您只需设置一次TextView的内容。每次收到点击事件时都应该更新它们。具体做法是:

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

注意:如果您的变量current_number,record_number,cur_text和rec_text尚未被声明为类成员变量,那么您希望这样做,以便在您离开后可以访问它们你所做的所有这些方法的范围(我假设它是onCreate(...)

答案 2 :(得分:1)

您需要在此处执行的操作是在按钮的单击事件期间更新标签。您目前只在活动创建时更新它们。每次点击都不会执行此操作。我可以回答有关下面修复版本的任何问题吗?

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