当前位置: 代码迷 >> Android >> Android应用开发札记 - 项目代码1
  详细解决方案

Android应用开发札记 - 项目代码1

热度:38   发布时间:2016-05-01 13:28:25.0
Android应用开发笔记 - 项目代码1

1.1 Widget类

所有的源代码都位于com.example.prjandroid包下:

 

FullImageActivity.java:

package com.example.prjandroid;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.widget.ImageView;public class FullImageActivity extends Activity {	@Override	protected void onCreate(Bundle savedInstanceState) {		// TODO Auto-generated method stub		super.onCreate(savedInstanceState);		setContentView(R.layout.full_img1);				Intent intent = getIntent();		int position = intent.getExtras().getInt("id");		ImageAdapter imgAdapter = new ImageAdapter(FullImageActivity.this);				ImageView imgView = (ImageView) findViewById(R.id.imgView1);		imgView.setImageResource(imgAdapter.mThumbIds[position]);	}}

ImageAdapter.java:

package com.example.prjandroid;import android.content.Context;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.GridView;import android.widget.ImageView;public class ImageAdapter extends BaseAdapter {	private Context mContext;		public Integer[] mThumbIds = {		R.drawable.emacs1,		R.drawable.emacs2,		R.drawable.emacs3,		R.drawable.emacs4,		R.drawable.emacs5,		R.drawable.emacs6,		R.drawable.emacs7,		R.drawable.emacs8,		R.drawable.emacs9,		R.drawable.emacs10,		R.drawable.emacs11,		R.drawable.emacs12,		R.drawable.emacs13,		R.drawable.emacs14,		R.drawable.emacs15	};		public ImageAdapter(Context context) {		// TODO Auto-generated constructor stub		mContext = context;	}		@Override	public int getCount() {		// TODO Auto-generated method stub		return mThumbIds.length;	}	@Override	public Object getItem(int position) {		// TODO Auto-generated method stub		return position;	}	@Override	public long getItemId(int position) {		// TODO Auto-generated method stub		return 0;	}	@Override	public View getView(int position, View converView, ViewGroup parent) {		// TODO Auto-generated method stub		ImageView imgView = new ImageView(mContext);		imgView.setImageResource(mThumbIds[position]);		imgView.setScaleType(ImageView.ScaleType.CENTER_CROP);		imgView.setLayoutParams(new GridView.LayoutParams(70, 70));				return imgView;	}}
ImgAdapter.java:
package com.example.prjandroid;import android.content.Context;import android.content.res.TypedArray;import android.view.View;import android.view.ViewGroup;import android.view.ViewGroup.LayoutParams;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.GridView;import android.widget.ImageView;public class ImgAdapter extends BaseAdapter {	// resource draw	private int[] resPics = new int[] {		R.drawable.emacs1,		R.drawable.emacs2,		R.drawable.emacs3,		R.drawable.emacs4,		R.drawable.emacs5,		R.drawable.emacs6,		R.drawable.emacs7,		R.drawable.emacs8,		R.drawable.emacs9,		R.drawable.emacs10	};		private Context m_context;	private int m_galleryItemBackGround;		public ImgAdapter(Context context) {		// TODO Auto-generated constructor stub		m_context = context;		TypedArray typeArray = 				m_context.obtainStyledAttributes(R.styleable.galleryThem);		m_galleryItemBackGround = typeArray.getResourceId(				R.styleable.galleryThem_android_galleryItemBackground, 0);	}		@Override	public int getCount() {		// TODO Auto-generated method stub		return resPics.length;	}	@Override	public Object getItem(int position) {		// TODO Auto-generated method stub		return position;	}	@Override	public long getItemId(int position) {		// TODO Auto-generated method stub		return position;	}	@Override	public View getView(int position, View convertView, ViewGroup parent) {		// TODO Auto-generated method stub		ImageView imgView = new ImageView(m_context);			imgView.setImageResource(resPics[position]);			imgView.setScaleType(ImageView.ScaleType.FIT_XY);			imgView.setLayoutParams(new Gallery.LayoutParams(163, 106));						imgView.setBackgroundResource(m_galleryItemBackGround);				return imgView;	}}
MainActivity.java
package com.example.prjandroid;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.view.Gravity;import android.view.LayoutInflater;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.AdapterView.OnItemClickListener;import android.widget.AdapterView.OnItemSelectedListener;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.AutoCompleteTextView;import android.widget.Button;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.Gallery;import android.widget.GridView;import android.widget.ImageButton;import android.widget.ImageSwitcher;import android.widget.ImageView;import android.widget.ListView;import android.widget.MultiAutoCompleteTextView;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.RadioGroup.OnCheckedChangeListener;import android.widget.SimpleAdapter;import android.widget.Spinner;import android.widget.Toast;import android.widget.ViewSwitcher.ViewFactory;public class MainActivity extends Activity implements OnClickListener, OnCheckedChangeListener/* RadioButton */, OnItemSelectedListener/* Spinner */, android.widget.CompoundButton.OnCheckedChangeListener/* CheckBox */,ViewFactory/* ImageSwitcher */,OnItemClickListener/* GridView */ {	RadioButton m_radioBtn1, m_radioBtn2;		CheckBox m_chkBox1, m_chkBox2;		String m_str = "You Choice: ";		ArrayAdapter<CharSequence> m_adapterView;	Spinner m_spinner;		View m_view1, m_view2, m_view3, m_view4, m_viewList, m_viewArray, m_viewBase;	Boolean m_flag_view = false;	Boolean m_flag_view2 = false;	Boolean m_flag_view3 = false;	Boolean m_flag_viewList = false;	Boolean m_flag_viewArray = false;	Boolean m_flag_viewBase = false;		// resource draw	private int[] resPics = new int[] {		R.drawable.emacs1,		R.drawable.emacs2,		R.drawable.emacs3,		R.drawable.emacs4,		R.drawable.emacs5,		R.drawable.emacs6,		R.drawable.emacs7,		R.drawable.emacs8,		R.drawable.emacs9,		R.drawable.emacs10	};		private int index = 0;		ImageSwitcher m_imgSwticher;	    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);                // LayoutInflater inflater = getLayoutInflater();        LayoutInflater inflater = LayoutInflater.from(this);        // setContentView(R.layout.activity_main);        m_view1 = inflater.inflate(R.layout.activity_main, null);        m_view2 = inflater.inflate(R.layout.view_gallery, null);        m_view3 = inflater.inflate(R.layout.view_image_switcher, null);        m_view4 = inflater.inflate(R.layout.gridview02, null);        m_viewList = inflater.inflate(R.layout.list_view, null);        m_viewArray = inflater.inflate(R.layout.view_arrayadapter, null);        m_viewBase = inflater.inflate(R.layout.view_base, null);                setView(m_view1);                Button btnOk = (Button) findViewById(R.id.btnOk);        btnOk.setOnClickListener(this);                // ImageButton        ImageButton imgBtn1 = (ImageButton) findViewById(R.id.imgBtn1);                imgBtn1.setOnClickListener(this);                // Button 2        Button btnGo = (Button) findViewById(R.id.btnGrid);        btnGo.setOnClickListener(this);                // ListView        Button btnListView = (Button) findViewById(R.id.btnListView);        btnListView.setOnClickListener(this);                // AutoComplete        Button btnViewArrayAdapter = (Button) findViewById(R.id.btnArrayAdapter);        btnViewArrayAdapter.setOnClickListener(this);                // Base View        Button btnBaseView = (Button) findViewById(R.id.btnBaseView);        btnBaseView.setOnClickListener(this);    }	    //Button    @Override    public void onClick(View v) {    	switch (v.getId()) {		case (R.id.btnOk):			setView(m_view2);			if (!m_flag_view) {		        // view_gallery is button		        Button btnReturn = (Button) findViewById(R.id.btnReturn);		        btnReturn.setOnClickListener(this);		        //		        // Gallery		        Gallery gallery = (Gallery) findViewById(R.id.gallery02);		        		        ImgAdapter imgAdapter = new ImgAdapter(MainActivity.this);		        		        gallery.setAdapter(imgAdapter);		        		        m_flag_view = true;			}						break;		case (R.id.btnReturn):			setView(m_view1);						break;					case (R.id.button1):			setView(m_view1);						break;				case (R.id.imgBtn1):			setView(m_view3);					if (!m_flag_view2) {				Button btnReturn2 = (Button) findViewById(R.id.button1);				btnReturn2.setOnClickListener(this);								Button btnPrev = (Button) findViewById(R.id.btnPrev);				btnPrev.setOnClickListener(this);								Button btnNext = (Button) findViewById(R.id.btnNext);				btnNext.setOnClickListener(this);								m_imgSwticher = (ImageSwitcher) findViewById(R.id.imgSwit01);				m_imgSwticher.setFactory(this);								m_flag_view2 = true;			}						break;					case (R.id.btnPrev):			index--;						setPicture(index);						break;				case (R.id.btnNext):			index++;						setPicture(index);						break;					case (R.id.btnGrid):			setView(m_view4);			if (!m_flag_view3) {				GridView gv = (GridView) findViewById(R.id.grid_view);				gv.setAdapter(new ImageAdapter(MainActivity.this));				gv.setOnItemClickListener(this);								m_flag_view3 = true;			}						break;				case (R.id.btnListView):			setView(m_viewList);			if (!m_flag_viewList) {				ListView listView = (ListView) findViewById(R.id.listView01);								List<Map<String, Object>> data = getSampleData();				SimpleAdapter sAdapter = new SimpleAdapter(						MainActivity.this, 						data, 						R.layout.list_row,						new String[] {							"imgIcon",							"title01",							"desText"						},						new int[] {							R.id.imgIcon,							R.id.title01,							R.id.desText						}				);								listView.setAdapter(sAdapter);								m_flag_viewList = true;			}						break;					case (R.id.btnArrayAdapter):			setView(m_viewArray);					if (!m_flag_viewArray) {				AutoCompleteTextView aCompTextView = 						(AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);								MultiAutoCompleteTextView mCompTextView = 						(MultiAutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextView1);								ArrayAdapter<String> arrayAdapter = 						new ArrayAdapter<String>(								MainActivity.this, 								android.R.layout.simple_dropdown_item_1line,								new String[] {									"ab",									"abc",									"abcd",									"abcde",									"abcdef",									"edcba",									"dcba",									"cba",									"ba",									"bcd"								});								aCompTextView.setAdapter(arrayAdapter);				mCompTextView.setAdapter(arrayAdapter);				mCompTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());								m_flag_viewArray = true;			}						break;				case (R.id.btnBaseView):			setView(m_viewBase);						if (!m_flag_viewBase) {			    // Spinner		        m_adapterView = ArrayAdapter.createFromResource(		        		MainActivity.this, 		        		R.array.bloods, 		        		android.R.layout.simple_spinner_dropdown_item);		        		        m_adapterView.setDropDownViewResource(		        		android.R.layout.simple_spinner_dropdown_item);		        		        m_spinner = (Spinner) findViewById(R.id.spin01);		        m_spinner.setAdapter(m_adapterView);		        m_spinner.setOnItemSelectedListener(MainActivity.this);		        		        // CheckBox		        m_chkBox1 = (CheckBox) findViewById(R.id.chkBox1);		        m_chkBox2 = (CheckBox) findViewById(R.id.chkBox2);		        		        m_chkBox1.setOnCheckedChangeListener(MainActivity.this);		        		        m_chkBox2.setOnCheckedChangeListener(MainActivity.this);		        		        // Radio		        RadioGroup radioGrou1 = (RadioGroup) findViewById(R.id.radioGroup01);		        m_radioBtn1 = (RadioButton) findViewById(R.id.radioBtn1);		        m_radioBtn2 = (RadioButton) findViewById(R.id.radioBtn2);		        		        radioGrou1.setOnCheckedChangeListener(MainActivity.this);		        		        m_flag_viewBase = true;			}						break;					default:			break;		}    }        // RadioButton    @Override    public void onCheckedChanged(RadioGroup group, int checkedId) {    	// TODO Auto-generated method stub    	if (checkedId == m_radioBtn1.getId()) {    		setToastString("Male").show();    	}    	else {    		setToastString("Female").show();    	}    }        // CheckBox    @Override    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {    	// TODO Auto-generated method stub    	switch (buttonView.getId()) {		case (R.id.chkBox1):			if (m_chkBox1.isChecked()) {				m_str += m_chkBox1.getText() + " ";				setToastString(m_str).show();			}					break;		case (R.id.chkBox2):			if (m_chkBox2.isChecked()) {				m_str += m_chkBox2.getText() + " ";				setToastString(m_str).show();			}					break;					default:			break;		}    }        // Spinner-1    @Override    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,    		long arg3) {    	// TODO Auto-generated method stub    	setToastString(    			// this.getResources().getStringArray(R.array.bloods)[arg2]    			m_spinner.getSelectedItem().toString()    					).show();    	arg0.setVisibility(arg1.VISIBLE);    }        // Spinner-2    @Override    public void onNothingSelected(AdapterView<?> arg0) {    	// TODO Auto-generated method stub    	    }        @Override    public void onItemClick(AdapterView<?> arg0, View arg1,     		int position, long arg3) {    	// TODO Auto-generated method stub    	Intent intent = new Intent(getApplicationContext(),     			FullImageActivity.class);    	intent.putExtra("id", position);    	startActivity(intent);    }        @Override    public View makeView() {    	// TODO Auto-generated method stub    	ImageView imgView = new ImageView(this);    	imgView.setScaleType(ImageView.ScaleType.FIT_CENTER);    	return imgView;    }        private List<Map<String, Object>> getSampleData() {    	List<Map<String, Object>> result = new ArrayList<Map<String,Object>>();    	    	for (int i = 0; i < 10; i++) {    		Map<String, Object> map = new HashMap<String, Object>();    		map.put("imgIcon", R.drawable.ic_launcher);    		map.put("title01", "image0" + i);    		map.put("desText", "This is image 0" + i);    		    		result.add(map);    	}    	    	return result;    }        /**     *      * @param str     * @return     */    private Toast setToastString (String str) {    	Toast toast = Toast.makeText(this, str, Toast.LENGTH_LONG);    	toast.setGravity(Gravity.BOTTOM, 0, 0);    	return toast;    }        /**     *      * @param v     */    private void setView(View v) {    	setContentView(v);    }        /**     *      * @param iIndex     */    private void setPicture(int iIndex) {    	if (iIndex < 0) {    		iIndex = resPics.length - 1;    	}    	if (iIndex >= resPics.length) {    		iIndex = 0;    	}    	m_imgSwticher.setImageResource(resPics[iIndex]);    	    }        @Override    public boolean onCreateOptionsMenu(Menu menu) {        getMenuInflater().inflate(R.menu.activity_main, menu);        return true;    }}

---------------xiaobin-author--



  相关解决方案