开始读《Android移动开发入门进阶》书的学习小结。
1 <Button android:text="@+id/Button01"> 当新的一个button时,必须要@告诉XML解析器,解析ID字符后的部分,当引用一个android id时,就不需要+号了,直接这样: android:id="@android:id/Button01
代码中去获得一个button: Button mybutton=(Button)findViewById(R.id.my_button);
2 TABLELAYOUT的例子: <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:stretchColumns="1">
<TableRow> <TextView android:text="用户名:" android:textStyle="bold" android:gravity="right" android:padding="3dip" />
<EditText android:id="@+id/username" android:padding="3dip" android:scrollHorizontally="true" /> </TableRow>
<TableRow> <TextView android:text="登录密码:" android:textStyle="bold" android:gravity="right" android:padding="3dip" />
<EditText android:id="@+id/password" android:password="true" android:padding="3dip" android:scrollHorizontally="true" /> </TableRow>
<TableRow android:gravity="right">
<Button android:id="@+id/cancel" android:text="取消" />
<Button android:id="@+id/login" android:text="登录" /> </TableRow></TableLayout>3 relative_layout的例子 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/blue" android:padding="10dip">
<TextView android:id="@+id/label" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="请输入用户名:" />
<!-- 这个EditText放置在上边id为label的TextView的下边 --> <EditText android:id="@+id/entry" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@android:drawable/editbox_background" android:layout_below="@id/label" />
<!-- 取消按钮和容器的右边齐平,并且设置左边的边距为10dip --> <Button android:id="@+id/cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/entry" android:layout_alignParentRight="true" android:layout_marginLeft="10dip" android:text="取消" />
<!-- 确定按钮在取消按钮的左侧,并且和取消按钮的高度齐平 --> <Button android:id="@+id/ok" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toLeftOf="@id/cancel" android:layout_alignTop="@id/cancel" android:text="确定" />
</RelativeLayout>
4 处理用户的点击事件,这个简单 button0 = (Button) findViewById(R.id.button0); button0.setOnClickListener(listener0);listener0 = new OnClickListener() { public void onClick(View v) { Intent intent0 = new Intent(ActivityMain.this, ActivityFrameLayout.class); setTitle("FrameLayout"); startActivity(intent0); } };
5 访问res/drawable目录下名叫a1的图片资源 Drawable d=this.getResources().getDrawable(R.drawable.a1);
6 MENU的操作 public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); menu.add(0, ITEM0, 0, "显示button1"); menu.add(0, ITEM1, 0, "显示button2"); menu.findItem(ITEM1); return true; } public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case ITEM0: actionClickMenuItem1(); break; case ITEM1: actionClickMenuItem2(); break;
} return super.onOptionsItemSelected(item);}
7 取得前一个ACTIVETY传过来的值的方法: Intent intent = getIntent(); String name = (String) intent.getExtras().get("name"); 比如在一个文本框中输入了值NAME,点BUTTON1弹出一个对话框,把NAME值传过去,这时可以这样: btn_dialog.setOnClickListener(new OnClickListener() { public void onClick(View v) { //通过Intent传递参数 Intent intent = new Intent(); intent.putExtra("name", editText.getText().toString()); //启动另一个Activity intent.setClass(MultiActivity.this, AlertDialog.class); startActivity(intent); } }); 用putExtra放参数的值,用startActivety启动一个新的activety
本文来自博客,转载请标明出处:http://blog.csdn.net/jackyrongvip/archive/2010/01/15/5195687.aspx