Android学习--基本的控件
前言:
这篇把自己今天看过的控件基本的敲了一遍,后序打算研究一下他们的基本的自定义,代码给出来,方便后买自己再学习看看,熟练了应该也就用不着了:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | <?xml version= "1.0" encoding= "utf-8" ?> <RelativeLayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http://schemas.android.com/tools" android:layout_width= "match_parent" android:layout_height= "match_parent" android:paddingBottom= "@dimen/activity_vertical_margin" android:paddingLeft= "@dimen/activity_horizontal_margin" android:paddingRight= "@dimen/activity_horizontal_margin" android:paddingTop= "@dimen/activity_vertical_margin" tools:context= "com.example.mxsm.uiwidgettest.MainActivity" > // match_parent 表示当前控件的大小和父控件大小是一样的 // wrap_content 表示当前控件的大小要适配内容 // textSize 字体的大小 // gravity 内容的对齐方式 <!--<TextView--> <!--android:layout_width= "match_parent" --> <!--android:layout_height= "wrap_content" --> <!--android:text= "Hello World!" --> <!--android:textSize= "24sp" --> <!--android:gravity= "center" --> <!--/>--> // 注意这个textAllCaps属性,禁止你写的Button按钮的字母被转成大写 // background 这个属性可以设置图片也可以设置颜色 // drawableLeft 设置左边图片,右边的类推 <Button android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:textAllCaps= "false" android:text= "我是一个按钮" android:background= "@color/colorPrimary" android:drawableLeft= "@color/colorAccent" android:id= "@+id/Image_button" /> // EditText这个控件 hind属性是设置一点提示文字,其他的基本属性类似 // maxLines 文本输入的最大行数,要是超过这个行数,就会向上滚动 <!--<EditText--> <!--android:layout_width= "wrap_content" --> <!--android:layout_height= "wrap_content" --> <!--android:hint= "这是一个输入框" --> <!--android:maxLines= "2" --> <!--/>--> <!-- // src 指定图片的路径--> <!-- // visibility 设置视图的可见或者不可见,有三个值分别是 gone invisible visible--> <!-- // invisible 表示视图不可见,其实就和设置透明效果了一样,但还是存在的--> <!-- // gone 也是不可见,不过它是不在占用任何的屏幕空间--> <!--<ImageView--> <!--android:layout_width= "wrap_content" --> <!--android:layout_height= "wrap_content" --> <!--android:src= "@mipmap/ic_launcher" --> <!--/>--> <!-- // 注意下面的Style是把它指定成水平的进度条--> <!--<ProgressBar--> <!--android:layout_width= "wrap_content" --> <!--android:layout_height= "wrap_content" --> <!--style= "?android:attr/progressBarStyleHorizontal" --> <!--android:max= "100" --> <!--/>--> // AlertDialog 提示框 dialog英文是对话框的意思 // ProgressDialog 带加载提示的提示框 // 以上总结的控件 </RelativeLayout> |
上面是XMl的布局文件,下面是活动的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | package com.example.mxsm.uiwidgettest; import android.app.ProgressDialog; import android.content.DialogInterface; import android.preference.DialogPreference; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity implements View.OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button = (Button) findViewById(R.id.Image_button); button.setOnClickListener( this ); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.Image_button: // setPositiveButton 这个方法添加一个确定按钮 // setNegativeButton 这个方法添加一个取消按钮 AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity. this ); alertDialog.setTitle( "这是一个弹出框" ); alertDialog.setMessage( "不是我说,你长的真的挺丑的!" ); // 这句话的意思是点击返回键是否能取消掉提示框 alertDialog.setCancelable( true ); alertDialog.setPositiveButton( "确定" , new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); alertDialog.setNegativeButton( "取消" , new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); alertDialog.show(); break ; default : break ; } } } |
## 努力做一个合格的程序员。
分类:
Android 日常总结
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话