最後一個ImageButton不起作用,當我交換最後一個ImageButton與第二個ImageButton它的工作,但最後一個現在不工作,所以我相信我的按鈕註冊正確,除了最後一個,只要我換周圍的最後一個永遠不工作,我有沒有隱藏的佈局,或諸如此類的事:最後ImageButton不工作
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:gravity="center|left"
android:text="Choose your option(s) below:"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="italic"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="30dp"
android:orientation="horizontal"
android:layout_margin="10dp"
android:layout_weight="1"
>
<ImageButton
android:id="@+id/ibSingle"
android:layout_width="20dp"
android:layout_height="20dp"
android:adjustViewBounds="true"
android:src="@drawable/radio_empty"
android:layout_gravity="center"
android:layout_margin="20dp"
android:scaleType="fitCenter"
android:background="@color/transparent"
/>
<TextView
android:id="@+id/tvSingle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Single"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="30dp"
android:orientation="horizontal"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:layout_weight="1"
>
<ImageButton
android:id="@+id/ibSeeking"
android:layout_width="20dp"
android:layout_height="20dp"
android:adjustViewBounds="true"
android:src="@drawable/radio_empty"
android:layout_gravity="center"
android:layout_margin="20dp"
android:scaleType="fitCenter"
android:background="@color/transparent"
/>
<TextView
android:id="@+id/tvSeeking"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Seeking"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="30dp"
android:orientation="horizontal"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:layout_weight="1"
>
<ImageButton
android:id="@+id/ibDrinking"
android:layout_width="20dp"
android:layout_height="20dp"
android:adjustViewBounds="true"
android:src="@drawable/radio_empty"
android:layout_gravity="center"
android:layout_margin="20dp"
android:scaleType="fitCenter"
android:background="@color/transparent"
/>
<TextView
android:id="@+id/tvDrinking"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Drinking"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="30dp"
android:orientation="horizontal"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:layout_weight="1"
>
<ImageButton
android:id="@+id/ibSmoking"
android:layout_width="20dp"
android:layout_height="20dp"
android:adjustViewBounds="true"
android:src="@drawable/radio_empty"
android:layout_gravity="center"
android:layout_margin="20dp"
android:scaleType="fitCenter"
android:background="@color/transparent"
/>
<TextView
android:id="@+id/tvSmoking"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Smoking"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
</LinearLayout>
</LinearLayout>
注意「吸菸」的ImageButton是最後一個,如果我換它以「飲用」爲例然後吸菸工作和喝酒不起作用
JAVA:
ibSingle = (ImageButton) findViewById(R.id.ibSingle);
ibSeeking = (ImageButton) findViewById(R.id.ibSeeking);
ibDrinking = (ImageButton) findViewById(R.id.ibDrinking);
ibSmoking = (ImageButton) findViewById(R.id.ibSmoking);
sendSingle = "0";
sendSeeking = "0";
sendDrinking = "0";
sendSmoking = "0";
ibSingle.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (sendSingle.equals("0"))
{
ibSingle.setImageResource(R.drawable.radio_fill);
sendSingle = "1";
}
else if (sendSingle.equals("1"))
{
ibSingle.setImageResource(R.drawable.radio_empty);
sendSingle = "0";
}
}
});
ibSeeking.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (sendSeeking.equals("0"))
{
ibSeeking.setImageResource(R.drawable.radio_fill);
sendSeeking = "1";
}
else if (sendSeeking.equals("1"))
{
ibSeeking.setImageResource(R.drawable.radio_empty);
sendSeeking = "0";
}
}
});
ibDrinking.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (sendDrinking.equals("0"))
{
ibDrinking.setImageResource(R.drawable.radio_fill);
sendDrinking = "1";
}
else if (sendDrinking.equals("1"))
{
ibDrinking.setImageResource(R.drawable.radio_empty);
sendDrinking = "0";
}
}
});
ibSmoking.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (ibSmoking.equals("0"))
{
Toast.makeText(getApplicationContext(), "last-button clicked", Toast.LENGTH_SHORT).show();
ibSmoking.setImageResource(R.drawable.radio_fill);
sendSmoking = "1";
}
else if (sendSmoking.equals("1"))
{
Toast.makeText(getApplicationContext(), "last-button clicked", Toast.LENGTH_SHORT).show();
ibSmoking.setImageResource(R.drawable.radio_empty);
sendSmoking = "0";
}
}
});
是的,我更喜歡編寫自己的手動收音機或複選框按鈕,而不是使用內置方法。
除了最後一個ImageButton(無論哪個放在最後一個)之外,一切都按預期工作,它只會發出一聲咔噠聲。我試圖添加一個firth虛擬ImageButton,現在最後2個ImageButton('虛擬'和'吸菸')不起作用,如果真的那麼我會讓'虛擬'看不見,但太糟糕了,黑客無法正常工作。
不應該是'if(sendSmoking.equals(「0」))'而不是if'(ibSmoking.equals(「0」))'? – Prexx
@Prexx感謝我的錯誤,深夜編碼讓我錯過了,謝謝先生 – user3560827