Android中的集中常用布局方式和按钮事件的处理方式

一、Android中监听点击事件的三种方式

// 方式一Button button = (Button) findViewById(R.id.button1);button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubSystem.out.println("按钮被点击了");}});
// 方式二// 实现OnClickListener接口Button btn2 = (Button) findViewById(R.id.button2);btn2.setOnClickListener(this);// 实现接口方法@Overridepublic void onClick(View v) {    // TODO Auto-generated method stub    System.out.println("点击了按钮");}
   
// 方式三  button在layout中绑定事件onClick="clickButton"  包括多个按钮触发同一个方法根据ID来判断点击的按钮    public void clickButton(View v) {    int btnId = v.getId();switch (btnId) {case R.id.button1:System.out.println("button3");break;case R.id.button2:System.out.println("button4");break;case R.id.button3:System.out.println("button5");break;}}

二、Android中的几种布局方式

1、相对布局

RelativeLayout
组件默认左对齐、顶部对齐
设置组件在指定组件的右边
android:layout_toRightOf="@id/tv1"
设置在指定组件的下边/上边
android:layout_below="@id/tv1"
android:layout_above="@id/tv1"
设置右对齐父元素
android:layout_alignParentRight="true"
设置与指定组件右对齐
android:layout_alignRight="@id/tv1"
设置在父控件的中心
android:layout_centerInParent="true"

2、线性布局
LinearLayout
指定各个节点的排列方向
android:orientation="horizontal" // 水平
android:orientation="vertical" // 垂直
设置右对齐
android:layout_gravity="right"
当竖直布局时,只能左右对齐和水平居中,顶部底部对齐竖直居中无效
当水平布局时,只能顶部底部对齐和竖直居中
使用match_parent时注意不要把其他组件顶出去
线性布局非常重要的一个属性:权重
android:layout_weight="1"
权重设置的是按比例分配剩余的空间
3、帧布局
FrameLayout
 默认组件都是左对齐和顶部对齐,每个组件相当于一个div
 可以更改对齐方式
android:layout_gravity="bottom"
不能相对于其他组件布局
4、表格布局
TableLayout
每个节点是一行,它的每个子节点是一列
表格布局中的节点可以不设置宽高,因为设置了也无效
根节点的子节点宽为匹配父元素,高为包裹内容
节点的子节点宽为包裹内容,高为包裹内容
以上默认属性无法修改
设置列数
android:layout_column="1"
根节点中可以设置以下属性,表示让第1列拉伸填满屏幕宽度的剩余空间
android:stretchColumns="1"
5、绝对布局
AbsoluteLayout
直接指定组件的x、y坐标
android:layout_x="100dp"
android:layout_y="100dp"

三、拨打电话方式

添加权限:

// 创建意图对象Intent intent = new Intent();// 把动作封装到意图对象中intent.setAction(intent.ACTION_CALL);// 设置打给谁intent.setData(Uri.parse("tel:" + phoneNumberString));// 把动作告诉系统startActivity(intent);

四、发送短信

需要添加权限:

<uses-permissionandroid:name="android.permission.SEND_SMS"/>

// 获取电话号码和内容EditText phoneText = (EditText) findViewById(R.id.phoneNumber);EditText contentText = (EditText) findViewById(R.id.content);String number = phoneText.getText().toString();String content = contentText.getText().toString();SmsManager message = SmsManager.getDefault();ArrayList messageArray = message.divideMessage(content);// 短信内容过长,分条发送for (String msg : messageArray) {   // 发送   message.sendTextMessage(number, null, msg, null, null);}



更多相关文章

  1. android RelativeLayout(相对布局)详细说明
  2. 如何通过代码更改ANDROID的UI布局
  3. Android之布局属性
  4. Android开发之ConstraintLayout布局
  5. android布局学习利器-Hierarchy Viewer
  6. android相对布局的案例
  7. UI组件之AdapterView及其子类(二)GridView网格视图的使用

随机推荐

  1. A Visual Guide to Android(安卓)GUI Wid
  2. Android中各种onTouch事件
  3. Android封装Toast工具类
  4. Android(安卓)gradle测试
  5. android ftp服务器实现
  6. [置顶] Android中显示AlertDialog对话框
  7. 分页控件1
  8. android ClassNotFoundException: Didn't
  9. 监听Bluetooth
  10. Android拷贝图片到指定文件路径