我正在做一个烹饪应用程序,我创建了一个最喜欢的按钮,似乎工作正常我点击它并改变颜色。我尝试创建一个吐司,当我点击按钮时,会有一条消息说出来
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>
我已经使用了收藏夹按钮的复选框,这可能就是吐司不能正常工作的原因
也有人能够告诉我如何制作代码,这样一旦你点击了收藏夹按钮就会保持点击状态,因为它不会一直点击
提前致谢
答案 0 :(得分:0)
我建议您使用OnClickListener
更改OnCheckedChangeListener
。当用户检查/取消选中CheckBox的状态时,它将被调用。恕我直言,它是这个小部件更好的倾听者。
答案 1 :(得分:0)
尝试以下代码
if ((fav.isChecked()) {
Toast.makeText(Favourite_Toast.this, "Added to Favourites", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(Favourite_Toast.this, "Removed From Favourites", Toast.LENGTH_LONG).show();
}
答案 2 :(得分:0)
在MainActivity中使用此代码......这将解决您的问题:
public class MainActivity extends Activity {
private CheckBox chk;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chk = (CheckBox) findViewById(R.id.checkBox1);
chk.setOnClickListener(checkboxClickListener);
}
View.OnClickListener checkboxClickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
boolean checked = ((CheckBox) view).isChecked();
if (checked) {
Toast.makeText(MainActivity.this,"Added to favourite", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(MainActivity.this,"Removed from favourite", Toast.LENGTH_LONG).show();
}
}
};
}