实验3(Button程序设计)
(触发文字改变、循环改变图片、动态添加按钮)
public class Ex03Activity extends Activity {
private Button mBtn;
private TextView mTxt;
private ImageButton mImageBtn;
boolean flag=true;
private Button mBtn1;
private Context mContext;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.main);
mTxt = (TextView)findViewById(HelloWorld);
mBtn = (Button)findViewById(R.id.Btn1);
O nClickListener Btn1_Listener = new OnClickListener()
{
public void onClick(View v){
mTxt.setText("按下了"+String.valueOf(((Button)v).getText()));
}
};
m Btn.setOnClickListener(Btn1_Listener);
m ImageBtn = (ImageButton)findViewById(R.id.ImageBtn2);
O nClickListener ImageBtn2_Listener = new OnClickListener()
{
public void onClick(View v){
if(flag){
mImageBtn.setImageDrawable(getResources().getDrawable(R.drawable.state2));
flag=false;
}else{
mImageBtn.setImageDrawable(getResources().getDrawable(R.drawable.state1));
flag=true;
}
}
};
m ImageBtn.setOnClickListener(ImageBtn2_Listener);
m Context = this;
mBtn1=new Button(mContext);
mBtn1.setText("动态添加按钮");
LinearLayout mLinearLatout=(LinearLayout)findViewById(R.id.dongtai);
m LinearLatout.addView(mBtn1);
O nClickListener mBtn1_Listener = new OnClickListener()
{
public void onClick(View v){
Toast toast = Toast.makeText(mContext, "动态添加按钮", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.show();
}
};
m Btn1.setOnClickListener(mBtn1_Listener);
}
}
(输入帐号密码触发图像改变)
public class MainActivity extends Activity {
private ImageButton but1;
private Button but2;
private EditText user;
private EditText password;
private Context mContext;
private TextView text;
@Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
mContext = this;
text=new TextView(mContext);
text.setText("动态添加TextView");
LinearLayout mLinearLatout=(LinearLayout)findViewById(R.id.out);
mLinearLatout.addView(text);
but1=(ImageButton)findViewById(R.id.imageButton1);
OnClickListener imageButton1_Listener = new OnClickListener(){
public void onClick(View v){
user=(EditText)findViewById(R.id.editText1);
password=(EditText)findViewById(R.id.editText2);
if((Text().toString().equals("android"))&&(Text().toString().equa ls("b403")))
but1.setImageDrawable(getResources().getDrawable(03state1));
else
but1.setImageDrawable(getResources().getDrawable(03state2));
}
};
but1.setOnClickListener(imageButton1_Listener);
but2=(Button)findViewById(R.id.button2);
user=(EditText)findViewById(R.id.editText1);
password=(EditText)findViewById(R.id.editText2);
OnClickListener Button2_Listener = new OnClickListener(){
public void onClick(View v){
but1.setImageDrawable(getResources().getDrawable(drawable.sym_def_app_icon));
user.setText("");
password.setText("");
}
};
but2.setOnClickListener(Button2_Listener);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().u.activity_main, menu);
return true;
}
}
(动态添加电话号码)
public class MainActivity extends Activity implements OnClickListener { private Button button;
private EditText numberText;
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
//获取最外层的LinearLayout
LinearLayout outter = (LinearLayout) findViewById(R.id.LinearLayout1);
//循环嵌套添加12个按钮
for (int i = 0; i < 4; i++) {
//每3个按钮动态添加一层水平的LinearLayout
LinearLayout inner = new LinearLayout(this);
inner.setOrientation(LinearLayout.HORIZONTAL);
inner.setLayoutParams((new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT)));
//在每个内层的LinearLayout添加3个按钮
for (int j = 1; j <= 3; j++) {
button = new Button(this);
if (i < 3)
button.setText("" + (i * 3 + j));
else if (j == 1)
button.setText("*");
else if (j == 2)
button.setText("0");
else
button.setText("#");
//设置每个按钮的大小,使其平均分配一层空间
button.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT, 1));
inner.addView(button);
button.setOnClickListener(this);
}
outter.addView(inner);
}
//增加清除按钮
button = new Button(this);
button.setText("清除");
outter.addView(button);
button.setOnClickListener(this);
}
public void onClick(View v) {
//获取按钮按下前的号码数值
numberText=(EditText) findViewById(R.id.editText1);
numberText.setCursorVisible(false);
//若按下清空按钮,清除号码栏
if (((Button) v).getText().equals("清除"))
numberText.setText("");
//否则在后面加上该按钮上的数字
else
numberText.Text().toString()
+ ((Button) v).getText().toString());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().u.activity_main, menu);
return true;
}
}
实验4(Intent、Bundle的使用以及ListView的应用)
(页面跳转,实现一个列表:张三李四王五......)
(不要忘记在Manifest中注册所有新建的activity!!!)
T extActivity.java
public class TestActivity extends Activity {
private List<Map<String, String>> mDataList= new ArrayList<Map<String, String>>();
private void setData() {
Map<String, String> map = new HashMap<String, String>();
map.put("name", "张三");
map.put("class", "数媒");
mDataList.add(map);
map = new HashMap<String, String>();
map.put("name", "李四");
map.put("class", "通软");
mDataList.add(map);
map = new HashMap<String, String>();
map.put("name", "王五");
map.put("class", "计应");
mDataList.add(map);
map = new HashMap<String, String>();
map.put("name", "朱六");
安卓课程设计源代码map.put("class", "嵌软");
mDataList.add(map);
map = new HashMap<String, String>();
map.put("name", "卓七");
map.put("class", "电政");
mDataList.add(map);
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.main);
ListView mListView = (ListView) findViewById(R.id.listView1);
setData();
SimpleAdapter listItemAdapter = new SimpleAdapter(this, mDataList,
R.layout.listview_item, new String[] { "name", "class" },

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。