樂學成語——顯示每個成語的詳細信息

1.新建佈局文件dialog_infos.xml。

<span style="font-size:18px;"><?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="wrap_content"
            android:layout_height="wrap_content"
            android:text="Medium Text" 
            android:textAppearance="?android:attr/textAppearanceMedium"
            />

    </LinearLayout>
    

</ScrollView></span>

2.新建DialogUtil類。

<span style="font-size:18px;">package com.edu.happyidiom.util;

import com.example.happyidiom.R;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

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_infos, null);
		builder.setView(view);
		TextView tvIdiomInfo=(TextView) view.findViewById(R.id.tvIdiomInfo);
		tvIdiomInfo.setText(result);
		builder.setPositiveButton("確定", new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				dialog.dismiss();
			}
		});
		builder.create().show();
	}</span>

3.修改StudyAnimalActivity的點擊事件。

<span style="font-size:18px;">package com.example.happyidiom;

import java.util.List;

import com.edu.happyidiom.adapter.AnimalAdapter;
import com.edu.happyidiom.dao.AnimalDao;
import com.edu.happyidiom.entity.Animal;
import com.edu.happyidiom.util.DialogUtil;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

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_study_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.getAutonym()+
				"\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();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.study_animal, menu);
		return true;
	}
	

}</span>
                                    現在可以運行一下,界面如下
                                         


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章