实例16--心理测试

隔了两个月,又重新开始将j2me转成android,7月8月,我先经过期末考,又到一家信息科技公司开发android手机客户端,或许是自己变懒了,所以就没有继续转,现在开学了,空余时间又开始多了,所以又开始了...两个月自己也成长了许多.

下面给出实例16--心理测试的主要代码,对于将j2me转成android,我有了新的看法,首先我要了解j2me的原理,并了解作者的思想,然后将这种思想在android中实现,而不单单是代码的转换.这次的例子我昨天晚上就要开始转,但一直在犹豫怎么转,最后我是将源代码仔细的看了一遍又一遍,然后用android自己写出来的,当然很多东西跟源代码是一样.

效果图:


实例16--心理测试


实例16--心理测试
 
实例16--心理测试

 

Demon_16Activity.java

package com.wjh.demon_16;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class Demon_16Activity extends Activity implements OnClickListener, OnCheckedChangeListener {
    /** Called when the activity is first created. */
	//type控制对话框的显示内容,
	//0显示所有题目的标题,1显示被选题目的问句及选项,2显示测试结果
	private static final int TEST_BUTTON_ID = 1;
	private static final int EXIT_BUTTON_ID = 2;
	private static final int RETURN_BUTTON_ID = 3;
	private int type = 0;
	//标题的控件
	private LinearLayout titlesLLayout;			
	private RadioGroup titlesRGroup;			
	private TextView titlesTView;					
	//选项的控件
	private LinearLayout optionsLayout;			
	private TextView optionsTView;				
	private RadioGroup optionRGroup;
	//结果的显示控件
	private TextView resultTView;				
	private ArrayList<TestItem> testItems;		//存储所有的题目
	private int titleItem = -1;					//当前选择的题目
	private int optionItem = -1;				//当前选择的选项
	private Button testButton;                 	//测试指令
	private Button exitButton;               	//退出指令
	private Button returnButton;			  	//返回按钮
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        getView();
        LoadText();
        
    }
    private void getView()
    {
    	//获得标题的控件
    	titlesLLayout = (LinearLayout)findViewById(R.id.title_ll);
    	titlesRGroup = (RadioGroup)findViewById(R.id.title_rg);
    	titlesRGroup.setOnCheckedChangeListener(this);
    	titlesTView = (TextView)findViewById(R.id.title_tv);
    	titlesTView.setText("心理测试");
    	//获得选项的控件
    	optionsLayout = (LinearLayout)findViewById(R.id.options_ll);
    	optionsTView = (TextView)findViewById(R.id.options_tv);
    	//获得按钮
    	testButton = (Button)findViewById(R.id.test_bt);
    	testButton.setId(TEST_BUTTON_ID);
    	testButton.setOnClickListener(this);
    	exitButton =(Button)findViewById(R.id.exit_bt);
    	exitButton.setId(EXIT_BUTTON_ID);
    	exitButton.setOnClickListener(this);
    	returnButton= (Button)findViewById(R.id.return_bt);
    	returnButton.setId(RETURN_BUTTON_ID);
    	returnButton.setOnClickListener(this);
    	resultTView = (TextView)findViewById(R.id.result_tv);
    	
    }
	private void LoadText(){
		testItems = new ArrayList<TestItem>();							//题目容器
		try{
			byte b[] = new byte[10240];						//分配10K大小的缓存	
			InputStream is = getResources().openRawResource(R.raw.test);
			int total = is.read(b);								//一次性将数据全部读出
			int start = 0;
			int index = 0;
			while( start < total )
			{//当 start < total 时,表明还有尚未读取的题目
				testItems.add(new TestItem(this));					//为新题目分配存储空间
				start = testItems.get(index).Load( b, start, total );	//读取一个题目的全部信息
				RadioButton rButton = new RadioButton(this);
				rButton.setText(testItems.get(index).Title);
				titlesRGroup.addView(rButton, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);	//保存题目的标题
				index ++;
			}
			is.close();
		}
		catch (IOException e){									//输出错误信息
			e.printStackTrace();
		}
	}
	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		switch(arg0.getId())
		{
		case TEST_BUTTON_ID:
			switch( type )
			{
				case 0:						//当前对话框上显示所有标题
					if( titleItem >= 0 )
					{		//如果选择某个题目,则更换显示内容
						if(optionsLayout!=null)
						{
						optionsLayout.removeView(optionRGroup);
						}
						optionRGroup = testItems.get(titleItem).choicegroup;
						optionsLayout.addView(optionRGroup);
						optionsTView.setText(testItems.get(titleItem).questionStr);
						optionRGroup.setOnCheckedChangeListener(this);
						titlesLLayout.setVisibility(View.GONE);
						optionsLayout.setVisibility(View.VISIBLE);
						type = 1;
					}
					break;
				case 1:						//当前对话框上显示问句和选项
					int n = optionItem;
					if( n >= 0 ){			//如果选择某个选项,则更换显示内容
						resultTView.setText(testItems.get(titleItem).results[n]);
						resultTView.setVisibility(View.VISIBLE);
						optionsLayout.setVisibility(View.GONE);
						type = 2;
						testButton.setVisibility(View.GONE);
						returnButton.setVisibility(View.VISIBLE);
					}
					break;
			}
			break;
		case RETURN_BUTTON_ID:
				resultTView.setVisibility(View.GONE);
				titlesLLayout.setVisibility(View.VISIBLE);
				returnButton.setVisibility(View.GONE);
				testButton.setVisibility(View.VISIBLE);
				type = 0;
				break;
		case EXIT_BUTTON_ID:
				this.finish();
		}
		
	}
	@Override
	public void onCheckedChanged(RadioGroup arg0, int arg1) {
		// TODO Auto-generated method stub
			for(int i = 0;i<arg0.getChildCount();i++)
			{
				RadioButton btn = (RadioButton)arg0.getChildAt(i);
				if(btn.isChecked())
				{
					if(arg0 == titlesRGroup)
					{
						titleItem = i;
					}else  if(arg0 == optionRGroup)
					{
						optionItem = i;
					}
				}
			}
		
	}
}

 源码包:Demon_16.zip

apk文件(后缀改为apk):Demon_16.zip

你可能感兴趣的:(android,apk,j2me,心理,心理测试)