乐学成语实现之五:显示每条成语的详细信息

       这一节我们将实现点击每条成语以对话框的形式显示该成语的详细信息。

      首先在layout下新建布局文件dialog_info.xml,代码如下所示:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/bg_ling"
        android:orientation="vertical" >
        <TextView 
            android:id="@+id/tvIdiomInfo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium"/>
    </LinearLayout>
    

</ScrollView>
        可以看到,最外层是ScrollView组件,当内容较多时会自动出现垂直滚动条。接下来需要修改StudyAnimalActivity,增加点击事件处理,代码如下所示:
package cn.edu.bztc.happyidiom.activity;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import cn.edu.bztc.happyidiom.R;
import cn.edu.bztc.happyidiom.adapter.AnimalAdapter;
import cn.edu.bztc.happyidiom.dao.AnimalDao;
import cn.edu.bztc.happyidiom.entity.Animal;

public class StudyAnimalActivity extends Activity {

	private List<Animal> animalList;
	private AnimalDao animalDao;
	private ListView lvAnimalList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_animal);
		initAnimals();
		lvAnimalList = (ListView) findViewById(R.id.lvAnimalList);
		AnimalAdapter animalAdapter = new AnimalAdapter(this,
				R.layout.animal_item, animalList);
		lvAnimalList.setAdapter(animalAdapter);
		lvAnimalList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapterView, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Animal animal = animalList.get(position);
				String result = animal.getName() + "\n" + animal.getPronounce()
						+ "\n【解释】:" + animal.getExplain() + "\n【近义词】:"
						+ animal.getHomoionym() + "\n【反义词】:"
						+ animal.getAntonym() + "\n【来源】:"
						+ animal.getDerivation() + "\n【示例】:"
						+ animal.getExamples();
				DialogUtil.showDialog(result,StudyAnimalActivity.this);
			}
		});
	}

	private void initAnimals() {
		// TODO Auto-generated method stub
		animalDao = AnimalDao.getInstance(this);
		animalList = animalDao.getAllAnimals();
	}
}
       这里DialogUtil.showDialog()方法是自定义的方法,在util包下新建DialogUtil类,代码如下所示:

package cn.edu.bztc.happyidiom.util;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import cn.edu.bztc.happyidiom.R;

public class DialogUtil {

	public static void showDialog(String result,Context context){
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		LayoutInflater layoutInflater = LayoutInflater.from(context);
		View view = layoutInflater.inflate(R.layout.dialog_info, null);
		builder.setView(view);
		TextView tvIdiomInfo = (TextView) view.findViewById(R.id.tvIdiomInfo);
		tvIdiomInfo.setText(result);
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				dialog.dismiss();
			}
		});
		builder.create().show();
	}
}
       可是运行是点击每个成语并不弹出对话框,通过google搜索发现是因为在列表项中如果出现类似与按钮这种能够获取焦点的组件时,就会出现无法单击每一列表项的情况。

       解决办法是,修改AnimalAdapter类,加入如下两句:

viewHolder.btnSave.setFocusable(false);
viewHolder.btnSave.setFocusableInTouchMode(false);
       现在可以运行一下,运行结果如图所示:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值