实验二 android基本控件实验

实验二 android基本控件实验报告

【目的】

熟悉Android常用控件的基本操作,掌握它们的基本使用方法。了解控件之间的构成关系,熟悉适配器的使用原理。 【要求】

1、 了解控件的继承关系; 2、 掌握适配器的使用;

3、 掌握信息提示的实现机制; 4、 实现基本界面。 【过程】

1. 新建一个Android工程ControlTest; 2. 编写布局文件,实现布局效果,如图:

源代码:

3. MainActivity代码:

import android.app.Activity; import android.content.ClipData; import android.content.Context; import android.os.Bundle; import android.view.Gravity;

import android.view.Menu; import android.view.View;

import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.Spinner; import android.widget.TextView; import android.widget.Toast;

import java.util.ArrayList; import java.util.Arrays; import java.util.List;

public class MainActivity extends Activity { private Button mButtonAdd;

private TextView mTextClass, mTextGrade, mTextId, mTextName; private ListView mListView; private Spinner mSpinnerCourse; private String mCourseList, mAdapter; private ArrayList mDataList; private ItemAdapter mListAdapter; private Toast toast;

private ItemAdapter.ViewHolder mViewHolder; Context mContext;

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.initView(); this.initFunction(); }

private void initFunction(){ }

public boolean onCreateOptionMenu(Menu menu){ getMenuInflater().inflate(R.menu.menu_main,menu); return true; }

public void initView() {

mButtonAdd = (Button) findViewById(R.id.but_sure);

mTextClass = (TextView) findViewById(R.id.editxt_class); mTextGrade = (TextView) findViewById(R.id.editxt_grade); mTextId = (TextView) findViewById(R.id.editxt_id); mTextName = (TextView) findViewById(R.id.editxt_name); mListView = (ListView) findViewById(R.id.course_listview); mSpinnerCourse = (Spinner) findViewById(R.id.course_spinner); List mCourseList =

Arrays.asList(this.getResources().getStringArray(R.array.course_list)); ArrayAdapter mAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, mCourseList); mSpinnerCourse.setAdapter(mAdapter); mDataList = new ArrayList();

mListAdapter = new ItemAdapter(this, mDataList); mListView.setAdapter(mListAdapter);

mButtonAdd.setOnClickListener(new View.OnClickListener() { @Override

public void onClick(View v) {

String name = mTextName.getText().toString(); String id = mTextId.getText().toString();

String classes = mSpinnerCourse.getSelectedItem().toString(); String grade = mTextGrade.getText().toString(); //将所输入的信息添加到mDataList中 mDataList.add(new Item(id, name, grade, classes));

//刷新listview mListAdapter.notifyDataSetChanged();

//创建toast提示添加成功 toast = Toast.makeText(getApplicationContext(),

\添加成功!\, Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER, 0, 0); toast.show();

} }); }

public class deleteListener implements View.OnClickListener{ int position;

public deleteListener(int position1){ position=position1; }

public void onClick(View v){ delItem(position); }

};

public void delItem(int position){ mDataList.remove(position);

mListAdapter.notifyDataSetChanged();

mListView.postInvalidate(); } }

4. itemAdapt代码:

import android.content.ClipData; import android.content.Context; import android.graphics.Color; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.TextView; import android.widget.Toast;

import java.util.ArrayList;

/** * Created by John on 2016/4/25. */ class ItemAdapter extends BaseAdapter{

ArrayList mArrayitem; LayoutInflater mInflater; Context mContext;

View.OnClickListener deletelistener; private ItemAdapter.ViewHolder mViewHolder; private Toast toast;

public ItemAdapter(Context mContext,ArrayList item){ mArrayitem=item; mContext=mContext;

mInflater=(LayoutInflater)

mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); }

class ViewHolder{

public TextView mTextClass;

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4