Android:按钮在单击时不会在模拟器中显示输出文本

时间:2012-02-21 09:59:30

标签: java android button textview

更新:我已将活动代码更改为我的最佳工作集。我目前的问题是,当单击一个按钮时,App force会在模拟器中关闭。我的Logcat说问题是在第38行,switch语句调用updateDate(),第64行设置当前日期的文本。

我创建了一个应用程序,其中有两个按钮,显示当前的日期和时间,在Android模拟器中打开并单击按钮下方没有显示的文本。我在按钮下方使用TextView来显示输出。这是我的main.xml和activity中的代码。

main.xml中

<?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:gravity="top"
    android:orientation="vertical" >

    <Button
        android:id="@+id/dayOf"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/buttonDay"
        />

    <Button
        android:id="@+id/clock"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/buttonTime" 
        />

    <TextView
        android:id="@+id/info"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/output"
        android:textSize="72dip"
        />

</LinearLayout>

活动

package omaxwell.CS211D.TimeAndDate;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.util.Calendar;
import android.widget.TextView;
/*
 * This is the activity for my Android App that gives you the date and time 
 */
//*******************TimeAndDateActivity*******************
public class TimeAndDateActivity extends Activity implements View.OnClickListener
{
    Button dayOf;
    Button clock;
    @Override
    //*******************onCreate*******************
    public void onCreate(Bundle b)
    {
        super.onCreate(b);
        setContentView(R.layout.main);

        clock = (Button)this.findViewById(R.id.clock);   //Time Button
        clock.setOnClickListener(this);

        dayOf = (Button)this.findViewById(R.id.dayOf);   //Date Button
        dayOf.setOnClickListener(this);
    }
        //*******************onClick*******************
        public void onClick(View v) //Called when one of the buttons above is clicked
        {
            switch(v.getId())
            {
                case R.id.clock:
                    updateTime();
                    break;
                case R.id.dayOf:
                    updateDate();
                    break;
            }

        }
            //*******************updateTime*******************
            private void updateTime()
            {
                Calendar d = Calendar.getInstance();
                d.getTime();

                TextView tv = (TextView) findViewById(R.string.output);
                tv.setText(d.getTime().toString());
            }
            //*******************updateDay*******************
            private void updateDate()
            {
                Calendar c = Calendar.getInstance();

                int day = c.get(Calendar.DAY_OF_MONTH);
                int month = c.get(Calendar.MONTH);
                int year = c.get(Calendar.YEAR);

                String date = String.valueOf(day) + "-" + String.valueOf(month) + "-" +String.valueOf(year);

                TextView tv = (TextView) findViewById(R.string.output);
                tv.setText(date);
            }


}

提前致谢:)

4 个答案:

答案 0 :(得分:0)

创建textView后,你也可以添加它......所以

TextView tv = new TextView(this);
                tv.setText(date);

获取内容查看并说,

contentLayout.addView(tv);

答案 1 :(得分:0)

看起来你正在updateDate()中创建一个新的Textview,但是你没有做任何事情。相反,只需更新现有的Textview:

TextView txt = (TextView) findViewById(R.id.info);
txt.setText(date);

答案 2 :(得分:0)

我认为你没有将OnClickListener设置为按钮

public void onCreate(Bundle b)
        {
           super.onCreate(b);
           setContentView(R.layout.main);
           clock = (Button)this.findViewById(R.id.clock);   //Time Button
           dayOf = (Button)this.findViewById(R.id.dayOf);   //Date Button
           clock.setOnClickListener(this);
           dayOf.setOnClickListener(this);

        }

答案 3 :(得分:0)

这应该有效:

package omaxwell.CS211D.TimeAndDate;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.util.Calendar;
import android.widget.TextView;
/*
 * This is the activity for my Android App that gives you the date and time 
 */
//*******************TimeAndDateActivity*******************
public class TimeAndDateActivity extends Activity implements View.OnClickListener
{
    Button dayOf;
    Button clock;
    TextView info;

    @Override
    //*******************onCreate*******************
    public void onCreate(Bundle b)
    {
        super.onCreate(b);

        this.setContentView(R.layout.main); // set content view firstly


        clock = (Button)this.findViewById(R.id.clock);   //Time Button
        dayOf = (Button)this.findViewById(R.id.dayOf);   //Date Button
        // set OnClickListener, you can also use the "android:click" attribute in the XML
        clock.setOnClickListener(this);
        dayOf.setOnClickListener(this);

        info = (TextView)this.findViewById(R.id.info);   //TextView
    }

        //*******************onClick*******************
        public void onClick(View v) //Called when one of the buttons above is clicked
        {
            switch(v.getId())
            {
                case R.id.clock:
                    updateTime();
                    break;  // don't forget break ! Otherwise, updateDate() will be execute

                case R.id.dayOf:
                    updateDate();
                    break;
            }

        }

            //*******************updateTime*******************
            private void updateTime()
            {
                Calendar d = Calendar.getInstance();
                d.getTime();

                // just update info
                info.setText(d.getTime().toString());

                // Following is wrong!
                //TextView tv = new TextView(this);
                //tv.setText(d.getTime().toString());
                //  setContentView(R.id.info);
            }

            //*******************updateDay*******************
            private void updateDate()
            {
                Calendar c = Calendar.getInstance();

                int day = c.get(Calendar.DAY_OF_MONTH);
                int month = c.get(Calendar.MONTH);
                int year = c.get(Calendar.YEAR);

                String date = String.valueOf(day) + "-" + String.valueOf(month) + "-" +String.valueOf(year);

                // just update info
                info.setText(d.getTime().toString());

                // Following is wrong!
                //TextView tv = new TextView(this);
                //tv.setText(date);
                // setContentView(R.id.info);
            }
}