有2个问题
问题在toggleButton按钮哪里,看注释说明
package com.imooc.basictestdemo; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.EditText; import android.widget.ImageView; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; import android.widget.Toast; import android.widget.ToggleButton; public class MainActivity extends Activity { private EditText name; private RadioGroup sex; private CheckBox hot, fish, sour; private SeekBar seekBar; private Button find; private ImageView imageView; private ToggleButton toggleButton; private List<Food> lists_food; private List<Food> lists_get; private Person person; private RadioGroupListener radioGroupListener; private boolean isFish; private boolean isHot; private boolean isSour; private CheckBoxListener checkBoxListener; private int price = 30; private SeekBarListener seekBarListener; private ButtonListener buttonListener; private int count; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_food); // 初始化控件 initView(); // 初始化数据 initData(); // 为控件添加监听器 setListener(); } private void initView() { name = (EditText) findViewById(R.id.et_name); sex = (RadioGroup) findViewById(R.id.rg_sex); hot = (CheckBox) findViewById(R.id.cb_hot); fish = (CheckBox) findViewById(R.id.cb_fish); sour = (CheckBox) findViewById(R.id.cb_sour); seekBar = (SeekBar) findViewById(R.id.sb_price); seekBar.setProgress(30); find = (Button) findViewById(R.id.btn_find); toggleButton = (ToggleButton) findViewById(R.id.tb_click); toggleButton.setChecked(true); imageView = (ImageView) findViewById(R.id.iv_pic); } private void initData() { person = new Person(); lists_get = new ArrayList<Food>(); lists_food = new ArrayList<Food>(); lists_food.add(new Food("麻辣香锅", 55, R.drawable.malaxiangguo, true, false, false)); lists_food.add(new Food("水煮鱼", 48, R.drawable.shuizhuyu, true, true, false)); lists_food.add(new Food("麻辣火锅", 80, R.drawable.malahuoguo, true, true, false)); lists_food.add(new Food("清蒸鲈鱼", 68, R.drawable.qingzhengluyu, false, true, false)); lists_food.add(new Food("桂林米粉", 15, R.drawable.guilin, false, false, false)); lists_food.add(new Food("上汤娃娃菜", 28, R.drawable.wawacai, false, false, false)); lists_food.add(new Food("红烧肉", 60, R.drawable.hongshaorou, false, false, false)); lists_food.add(new Food("木须肉", 40, R.drawable.muxurou, false, false, false)); lists_food.add(new Food("酸菜牛肉面", 35, R.drawable.suncainiuroumian, false, false, true)); lists_food.add(new Food("西芹炒百合", 38, R.drawable.xiqin, false, false, false)); lists_food.add(new Food("酸辣粉", 40, R.drawable.suanlatang, true, false, true)); } private void setListener() { radioGroupListener = new RadioGroupListener(); sex.setOnCheckedChangeListener(radioGroupListener); checkBoxListener = new CheckBoxListener(); fish.setOnCheckedChangeListener(checkBoxListener); hot.setOnCheckedChangeListener(checkBoxListener); sour.setOnCheckedChangeListener(checkBoxListener); seekBarListener = new SeekBarListener(); seekBar.setOnSeekBarChangeListener(seekBarListener); buttonListener = new ButtonListener(); find.setOnClickListener(buttonListener); toggleButton.setOnClickListener(buttonListener); } class RadioGroupListener implements OnCheckedChangeListener { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // 当用户选择当前的RadioGroup组的Button时被触发 switch (checkedId) { case R.id.rb_man: person.setSex("男"); break; case R.id.rb_woman: person.setSex("女"); break; } System.out.println("性别:" + person.getSex()); } } class CheckBoxListener implements android.widget.CompoundButton.OnCheckedChangeListener { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // 当控件状态改变时被触发 CheckBox cbBox = (CheckBox) buttonView; switch (cbBox.getId()) { case R.id.cb_fish: if (isChecked) { isFish = true; } else { isFish = false; } break; case R.id.cb_sour: if (isChecked) { isSour = true; } else { isSour = false; } break; case R.id.cb_hot: if (isChecked) { isHot = true; } else { isHot = false; } break; } System.out.println("当前喜好:" + " 辣:" + isHot + " 海鲜:" + isFish + " 酸:" + isSour); } } class SeekBarListener implements OnSeekBarChangeListener { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { price = seekBar.getProgress(); Toast.makeText(MainActivity.this, "价格:" + price,Toast.LENGTH_SHORT).show(); } } class ButtonListener implements OnClickListener { @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_find:// 寻找菜品按钮 count = 0; lists_get.clear(); checkData(); if (!lists_get.isEmpty()) {//判断lists_get是否为空 showView(); } else { Toast.makeText(MainActivity.this, "未找到相关菜品", Toast.LENGTH_SHORT).show(); imageView.setImageResource(R.drawable.ic_launcher); } toggleButton.setChecked(true); break; case R.id.tb_click:// toggleButton按钮 /* * 老师,这里跟我预想的有点不一样 * toggleButton.isChecked()这个方法是判断toggleButton是否为选中状态 * 我在点击寻找菜品按钮的时候就强制将toggleButton设置为true,那么第一次点击该按钮的时候, * 按照我的想法按钮应该是选中状态,给person添加姓名、性别、菜品信息后吐司才对,结果是反着来的,给我搞糊涂了。 * 您看,我加了个!,代码才能正常运行,这是什么原因呢? * * 还有一个问题,点击下一个按钮的时候明显能感到卡顿是因为什么原因? */ if (!toggleButton.isChecked()) { person.setName(name.getText().toString()); if (sex.getCheckedRadioButtonId() == R.id.rb_man) { person.setSex("男"); }else{ person.setSex("女"); } person.setFood(lists_get.get(count)); Toast.makeText(MainActivity.this, person.toString(), Toast.LENGTH_SHORT).show(); count++; } else { if (count < lists_get.size()) { showView(); } else { Toast.makeText(MainActivity.this, "因为已到末位,所以从头显示", Toast.LENGTH_SHORT).show(); lists_get.clear(); count = 0; checkData(); showView(); } } break; } } private void checkData() { // 找出菜品 for (int i = 0; i < lists_food.size(); i++) { Food food = lists_food.get(i); if ((food.getPrice() <= price) && (food.isFish() == isFish && food.isHot() == isHot && food .isSour() == isSour)) { lists_get.add(food); } } System.out.println("*********" + lists_get.size()); } //显示图片的方法 private void showView() { // count值为要显示lists_get列表中第几个菜品的图片 // count值从0开始,表示显示lists_get列表中第一次菜品的图片 imageView.setImageResource(lists_get.get(count).getPic()); } } }
46
收起
正在回答
3回答
布局文件没有什么问题,问题还是在java代码中,我单独把你的toggleButton在IDE中试过运行,没有出现你问题1中描述的问题,但是上述下一个点击后的代码中逻辑处理有点问题,你可以参照我的逻辑修改下代码再试试:
if (toggleButton.isChecked()) { //判断count的值,如果count值小于集合个数-1,则count自增,否则count设为0并提示信息; }else{ //为person设置数据并提示消息 }
吴跃民
2017-06-08 13:42:34
<Button android:id="@+id/btn_find" android:layout_width="305dp" android:layout_height="50dp" android:layout_gravity="center_horizontal" android:layout_margin="5dp" android:layout_marginLeft="36dp" android:text="寻找菜品" android:textSize="22sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="0.96" android:background="#FFD9EC" android:orientation="vertical" > <ImageView android:id="@+id/iv_pic" android:layout_width="match_parent" android:layout_height="0dp" android:layout_margin="40dp" android:layout_weight="2.84" android:src="@drawable/ic_launcher" /> <ToggleButton android:id="@+id/tb_click" android:layout_width="306dp" android:layout_height="50dp" android:layout_marginBottom="42dp" android:layout_marginLeft="24dp" android:background="#ADADAD" android:textOff="下一个" android:textOn="显示信息" android:textSize="22sp" /> </LinearLayout>
Android零基础入门2018版
- 参与学习 人
- 提交作业 5461 份
- 解答问题 7238 个
此次推出的专题为Android攻城狮培养计划的第一部分语法与界面基础篇,将带大家从0开始学习Android开发。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星