作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
嘿,
我是 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/
我是一名优秀的程序员,十分优秀!