問題描述:
在程式中建立了一個按鈕,如下:
[java]
<Button
android:id="@+id/admin_new_questions"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="See Asked Questions"
/>
<Button
android:id="@+id/admin_new_questions"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="See Asked Questions"
/>
我想讓這個按鈕只在某些情況下顯示:
[java]
if ( clause )
{
Button admin_see_questions = (Button)findViewById(R.id.admin_new_questions);
admin_see_questions.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
....
}
});
}
if ( clause )
{
Button admin_see_questions = (Button)findViewById(R.id.admin_new_questions);
admin_see_questions.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
....
}
});
}
不知道什麼原因,按鈕在任何情況下都顯示,如果clause是一個錯誤,監聽器是不能被監聽的。
如何讓按鈕只有在 clause 是true的情況下顯示呢?
謝謝!
解決方案:
[java]
<Button
android:id="@+id/admin_new_questions"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="See Asked Questions"
android:visibility="gone"
/>
if ( clause )
{
Button admin_see_questions = (Button)findViewById(R.id.admin_new_questions);
admin_see_questions.setVisibility(View.VISIBLE);
admin_see_questions.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
....
}
});
}
<Button
android:id="@+id/admin_new_questions"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="See Asked Questions"
android:visibility="gone"
/>
if ( clause )
{
Button admin_see_questions = (Button)findViewById(R.id.admin_new_questions);
admin_see_questions.setVisibility(View.VISIBLE);
admin_see_questions.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
....
}
});
}