gpt4 book ai didi

java - Android 按钮在启动线程后不显示

转载 作者:行者123 更新时间:2023-12-01 13:51:04 34 4
gpt4 key购买 nike

嘿,

我是 Android 开发新手,正在阅读书籍来熟悉它。这段代码在一本书上也有,但到目前为止还不起作用。代码很简单:这是布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Hello"/>

<Button
android:id="@+id/trigger"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press Me"/>

</LinearLayout>

这是 Java 代码:

private int buttonPress = 0;
TextView mButtonLabel;

private long mStartTime = 0L;
TextView mTimeLabel;

private Handler mHandler = new Handler();

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

if(mStartTime == 0L) {
mStartTime = SystemClock.uptimeMillis();
mHandler.removeCallbacks(mUpdateTimeTask);
mHandler.postDelayed(mUpdateTimeTask, 100);
}

mTimeLabel = (TextView)findViewById(R.id.text);
mButtonLabel = (TextView)findViewById(R.id.trigger);

Button startButton = (Button)findViewById(R.id.trigger);

startButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
mButtonLabel.setText("Megnyomva: " + ++buttonPress + " alkalommal");
}
});
}

private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
final long start = mStartTime;
long milis = SystemClock.uptimeMillis() - start;
int seconds = (int) (milis/1000);
int minutes = (seconds/60);
seconds = seconds % 60;

mTimeLabel.setText("" + minutes + ":" + String.format("%02d", seconds));
mHandler.postDelayed(this, 200);
}
};

@Override
protected void onPause() {
mHandler.removeCallbacks(mUpdateTimeTask);
super.onPause();
}

@Override
protected void onResume() {
super.onResume();
mHandler.postDelayed(mUpdateTimeTask, 100);
}

我的问题是,该按钮不知何故不可见。有什么建议吗?

谢谢,戴夫。

最佳答案

来自

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Hello"/>

<Button
android:id="@+id/trigger"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press Me"/>

</LinearLayout>

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Hello"/>

<Button
android:id="@+id/trigger"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press Me"/>

</RelativeLayout >

关于java - Android 按钮在启动线程后不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19947799/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com