作用:用于显示文本。
常用属性及用法:
<!--textSize指定文字大小, gravity指定子布局即文字布局, text指定内容, visibility用于指定组件是否可见,AndroID所有组件都有这个属性, 这个属性有三个值,visible(可见)、invisible(不可见)、gone(消失) invisible不可见但是会占空间,gone直接消失不占空间--><TextVIEw androID:ID="tv_test" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:gravity="center" androID:textSize="24sp" androID:textcolor="#00ff00" androID:visibility="visible" androID:text="This is TextVIEw"/>
二、button作用:按钮,响应点击事件。
常用属性及用法:
<!--textAllCaps设置文本是否全大写--><button androID:ID="@+ID/btn" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:textAllCaps="false" androID:text="button"/>
三、EditText作用:作为输入框,读取输入数据。
常用属性和用法:
<!--hint属性设置属性提示内容, maxlines设置最大行数--><EditText androID:ID="@+ID/edit_text" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:hint="输入提示" androID:maxlines="2"/>
通过EditText获取输入的内容:
EditText editText = (EditText) findVIEwByID(R.ID.edit_text);String inputText = editText.getText().toString(); //获取editText的输入内容
四、ImageVIEw作用:显示图像。
常用属性和用法:
<!--src设置图片的资源路径--><ImageVIEw androID:ID="@+ID/image_vIEw" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:src="@drawable/img_1"/>
在java代码中设置图片路径:
ImageVIEw imageVIEw = (ImageVIEw) findVIEwByID(R.ID.image_vIEw);imageVIEw.setimageResource(R.drawable.img_2);
五、Progressbar作用:用于进度显示,常用于下载文件的进度显示或音乐播放的进度显示条。
常用属性和方法:
<!--max设置进度条走满时表示的数值,一般为100; style设置进度条的样式,默认情况下为圆形;--><Progressbar androID:ID="@+ID/progress_bar" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:visibility="visible" androID:max="100" />
设置和获取进度条的进度:
Progressbar progressbar = (Progressbar) findVIEwByID(R.ID.progress_bar);int progress = progressbar.getProgress();progress = progress + 10;progressbar.setProgress(progress);
六、AlertDialog作用:d出一个对话框,用于显示提示信息,用于交互。
提示:AlertDialog采用的是建造者模式创建的。
通常用法:
AlertDialog.Builder dialog = new AlertDialog.Builder(context);dialog.setTitle("标题");//设置对话框标题dialog.setMessage("提示消息");//设置提示消息dialog.setCancelable(false);//设置是否能撤销dialog.setPositivebutton("OK", new DialogInterface.OnClickListener(){ //设置确定按钮的内容和监听事件 @OverrIDe public voID onClick(DialogInterface dialog, int which){ //设置确定按钮的监听事件 }});dialog.setNegativebutton("Cancel", new DialogInterface.OnClickListener(){ //设置取消按钮的内容和监听事件 @OverrIDe public voID onClick(DialogInterface dialog, int which){ //设置取消按钮的监听事件 }});dialog.show();//显示d窗,没有此语句,d窗不会显示
七、ProgressDialog作用:显示d窗,但其显示的内容是进度条,表示当前正在处理耗时的 *** 作。
常用用法:
ProgressDialog progressDialog = new ProgressDialog(context);progressDialog.setTitle("标题");progressDialog.setMessage("提示信息");progressDialog.setCancelable(true);progressDialog.show();
总结 以上是内存溢出为你收集整理的Android常用组件全部内容,希望文章能够帮你解决Android常用组件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)