现在的位置: 首页 > 综合 > 正文

Android学习笔记–控件&布局

2014年07月27日 ⁄ 综合 ⁄ 共 2643字 ⁄ 字号 评论关闭

1、启动发短信的代码

Uri uri = Uri.parse("smsto://13800000000");
Intent intent = new (Intent.ACTION_SENDTO,uri);
intent.putExtra("sms_body","The SMS text");
startActivity(intent);

二、常用控件:
Button TextView 分别为按钮和文本

1、EditTextView 文本编辑框:

得到控件的值

String value = editTextViewName.getText().toString();

2、菜单:

点击主菜单调用
onCreateOptionsMenu(Menu menu){
 menu.add(0,1,1,R.String.about);
}

点击菜单中的选项调用
onOptionsItemselected(MenuItem item){
 判断item.getItemId()
 {
  执行操作

 }
}
P.S:动态布局时,程序当中也对控件进行布局,并非所有布局文件都放在xml中声明
3、RadioGroup、RadioButton 单选按钮:

RadioGroup中包含若干RadioButton,
设置监听器:
new RadioGroup.onCheckedChangeListener{
 public void onCheckedChanged(RadioGroup group,int
checkedId){
  checkedId判断{
   执行操作
  }
 }
}

CheckBox 多选按钮:
设置监听器:
new CompoundButton.onCheckedChangeListener{
 public void onCheckedChanged(CompoundButton
buttonView,boolean is Checked){
  idChecked判断{
   执行操作
  }
 }
}
注意CheckBox和RadioGroup的监听器有所不同

Toast 弹出提示框:
Toast.makeText(Context context,String Show_str,int
time).show();

4、ProgressBar 进度条
布局文件属性:
visibility、style、id、layout_width、layout_height、max
对象 progressBar
progressBar.setProgress(i)
progressBar.setMax(200)
progressBar.setSecondProgress(i+10)
默认的进度条无法显示进行的状态

ListView:
activity应继承ListActivity
SimpleAdapter类添加存储数据
SimpleAdapter simpleAdapter = new
SimpleAdapter(this,list,R.layouput.user,new
String[]{"属性名1","属性名2",...},new
int[]{R.id.user_ip,R.id.user_name,...})
其中list为存放map的列表,map中存放属性名和属性值的键值对,R.layouput.user为一个布局文件,R.id.user_ip为布局文件中对应属性名1的控件

setListAdapter(simpleAdapter)

点击ListView中的item调用的函数(非监听模式)

onListItemClick(ListView l,View v,int postion,long id){
  // l 当前的Listview,v
当前点击的item,postion 当前item的位置,id
当前item的id 点击listView中的某一个item而执行的操作
}

 

三、布局初步

id、text、grivity(控件内容的布局)、textSize、backgroud、layout_width、layout_height、padding(left、right、top、buttom):内边距、margin(left、right、top、buttom):外边距、sigleLine(为true时,控件中的内容在同一行中显示)、layout_weight(在同一个标签级别下的控件相对大小)

线性布局

LinearLayout:

orientation:vertical、horizontal

表格布局

TableLayout:

stretchColumns指定拉伸列

TableRow(定义一行在其中按水平线性布局)

嵌套布局

<LinearLayout 线性纵向布局>

<LinearLayout 线性横向布局>

<TextView/>

<TextView/>

<TextView/>

</LinearLayout 线性横向布局>

<LinearLayout 线性纵向布局>

<TableLayout 表格布局>

<TableRow>

<TextView/>

<TextView/>

</TableRow>

<TableRow>

<TextView/>

<TextView/>

<TextView/>

</TableRow>

</TableLayout 表格布局>

</LinearLayout 线性纵向布局>

</LinearLayout 线性纵向布局>

相对布局

RelativeLayout

layout_above(放置在另一个控件的底部):@id/另一个控件的id、layout_below、layout_toleftof、layout_torightof;layout_alignBaseline(与另一个控件底部对齐)、layout_alignBottom、layout_alignLeft、layout_alignRight、layout_alignTop;layout_alignParentBottom(为真时控件底部与父控件的底部对齐)、layout_alignParentRight、layout_alignParentLeft、layout_alignParentTop;layout_centerHorrizental(为真时控件放置于水平方向的中央)、layout_centerItnParent(为真时控件放置于父控件水平和垂直方向的中央)、layout_centerVertical(为真时将控件放置在处置方向的中央)、

 

抱歉!评论已关闭.