[英]Android: Buttons don't show output text in emulator when clicked
更新:我已將活動代碼更改為我的最佳工作集。 我當前的問題是,單擊按鈕時,App強制在模擬器中關閉。 我的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);
}
}
提前致謝 :)
創建textView之后,您也添加了它。
TextView tv = new TextView(this);
tv.setText(date);
獲取內容查看並說,
contentLayout.addView(tv);
看起來您在updateDate()中創建了一個新的Textview,但是您並未對其進行任何操作。 相反,只需更新您現有的Textview:
TextView txt = (TextView) findViewById(R.id.info);
txt.setText(date);
我認為您沒有將OnClickListener設置為button
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);
}
這應該工作:
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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.