我正在做一个烹饪应用程序,我创建了一个喜欢的按钮,似乎工作好,我点击它,它会改变颜色。我试图创建一个敬酒那里当我点击按钮来西港岛线的消息多达说用喜爱的按钮烘烤Android
added to favourites
和
removed from favourites
我已经使用了土司的代码不会有错误,应用完美运行,但敬酒不出现
这里是我的代码,我已经使用
Toast.java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.Toast;
public class Favourite_Toast extends Activity {
private CheckBox fav;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recipe);
addListenerOnChk();
}
public void addListenerOnChk() {
fav = (CheckBox) findViewById(R.id.checkBox);
fav.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
Toast.makeText(Favourite_Toast.this,
"Added to Favourites", Toast.LENGTH_LONG).show();
}
}
});
}
}
favourite.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:drawable="@drawable/heart_red" />
<item android:state_checked="false"
android:drawable="@drawable/heart_grey" />
</selector>
recipe.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/jbsbackground2"
android:orientation="vertical" >
<ImageView
android:id="@+id/iv_detail"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:scaleType="centerCrop"
android:src="@drawable/barbecuedporkribs" />
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="10dp"
android:layout_below="@+id/iv_detail"
android:background="#3D3C3A" />
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/button1" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_below="@+id/scrollView1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="5dp"
android:text="@string/textView2"
android:layout_toLeftOf="@id/favourites"
android:textColor="#000000"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" />
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tvName"
android:textColor="#000000"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="italic"
android:layout_below="@+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checkBox"
android:button="@drawable/favourite"
android:checked="false"
android:layout_alignTop="@+id/textView2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="46dp"
android:layout_marginEnd="46dp"
android:clickable="true" />
<TextView
android:id="@+id/tvTD"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tvTD"
android:textColor="#000000"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold"
android:layout_below="@+id/checkBox"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="@+id/tvIngredients"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/tvTD"
android:layout_marginTop="2dp"
android:text="@string/tvIngredients"
android:textColor="#000000"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="italic" />
<TextView
android:id="@+id/tvK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/tvIngredients"
android:layout_marginTop="5dp"
android:text="@string/tvK"
android:textColor="#000000"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" />
<TextView
android:id="@+id/tvPreparation"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/tvK"
android:layout_marginTop="2dp"
android:text="@string/tvPreparation"
android:textColor="#000000"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="italic" />
</RelativeLayout>
</ScrollView>
</RelativeLayout>
我用于收藏夹按钮复选框会是为什么敬酒不工作
的原因也任何人都可以告诉我如何制作代码,以便一旦你点击最喜欢的按钮,它将保持点击,因为它不会留下cli提前
嘿感谢您的回复,我想你的代码,但仍然不得到烤面包... – john
任何错误,你越来越? – user5783725
,因为我测试了这个代码,它的工作正常,因为你的要求。 – user5783725