log.v("","");//在控制面板输出内容
1.1_MainActivity和activity_main的初识在MainActivity所在文件夹中可以创建任意“?Activity”文件作为后端逻辑代码文件同理在activity_main所在文件夹中可以创建任意“activity_?”文件作为前端页面控制代码文件1.2_Activity的清单文件简介
注意两个文件名的“?”部分要相互对应
@H_301_134@
1.3_几种重要文件的介绍1.4_基本布局的认识与使用AndroID中的界面是由两个文件组成的 一个是Activity的Java文件 还有一个是xml的布局文件.Java文件都放在java的文件下布局文件全部在res/layout下
layout下的文件都是xml文件
res下的文件不允许有大写字母
manifests文件夹下,AndroIDManifest.xml:清单文件。可以配置应用的第一个启动的Activity,注册Activity,修改应用图标及应用名称
values自定义变量名
colors.xml:
strings.xml:
在此文件中定义变量类似C语言中的#define
多语言支持方式drawable文件夹和mipmap文件夹
一般将APP的icon放在minmap文件夹下,其他图片资源放在drawable文件夹下
1.4.1_relativeLayout(相对布局)基本布局的认识:
VIEw:表示为AndroID视图
Java文件和xml文件都能编写VIEw
不管是Java还是xml文件最后都会变成Java文件的视图(其中java编写的视图和java课上学的视图一样,一般开发软件不用java编写视图)
VIEwGroup:表示为布局(在xml中布局可以嵌套)
布局控件可以放其它控件常用布局 linearLayout(线性布局)、relativeLayout(相对布局)、ConstraintLayout(约束布局)
如果只有一个子控件,则不需要写orIEntation属性,有多个必须要写,在新工具不一定
常用控件 button(按钮)、TextVIEw(文本)、EditText(输入框)
根布局需要加代码<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:tools="http://schemas.androID.com/tools" xmlns:app="http://schemas.androID.com/apk/res-auto"
其中xmlns:app只在相对布局中加
1.4.2_线性布局没有方向性,但是有Z轴,代码在后的Z轴值越大,即可以悬浮必须要参照物
1、布局本身为参照
androID:layout_centerHrizontal 水平居中
androID:layout_centerVertical 垂直居中
androID:layout_centerInparent 相对于父元素完全居中
androID:layout_alignParentBottom 贴紧父元素的下边缘
androID:layout_alignParentleft 贴紧父元素的左边缘
androID:layout_alignParentRight 贴紧父元素的右边缘
androID:layout_alignParenttop 贴紧父元素的上边缘2、通过ID来指定参照物
androID:layout_below 在某元素的下方
androID:layout_above 在某元素的的上方
androID:layout_toleftOf 在某元素的左边
androID:layout_toRightOf 在某元素的右边
androID:layout_aligntop 本元素的上边缘和某元素的的上边缘对齐
androID:layout_alignleft 本元素的左边缘和某元素的的左边缘对齐
androID:layout_alignBottom 本元素的下边缘和某元素的的下边缘对齐
androID:layout_alignRight 本元素的右边缘和某元素的的右边缘对齐
3. 属性值为具体的像素值,如 30dip,40px
androID:layout_marginBottom 离某元素底边缘的距离
androID:layout_marginleft 离某元素左边缘的距离
androID:layout_marginRight 离某元素右边缘的距离
androID:layout_margintop 离某元素上边缘的距离
如果只有一个子控件,则不需要写orIEntation属性,有多个必须要写,在新工具不一定在线性布局中
空间大小:androID:layout_wIDth="" androID:layout_height=""
match_parent只会占满剩余的空间 wrap_content适应文本大小 除此之外,还可以写固定数值,单位是dp或dip
layout_weight是权重
<linearLayout androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="horizontal" > <button androID:ID="@+ID/button_alterdialog" androID:layout_wIDth="0dp" androID:layout_height="wrap_content" androID:layout_weight="1" androID:text="删除" androID:onClick="delete" /> <button androID:layout_wIDth="0dp" androID:layout_height="wrap_content" androID:layout_weight="1" androID:text="增加" androID:onClick="delete" /> </linearLayout>
2_访问资源的方式2.1_java访问资源的方式R.resource_type.name//R可以理解为资源中的最高包AndroID.R.//官方资源
2.2_xml访问资源的方式@pakage_name:resource_type/name
3_控件的使用3.1_通用属性gravity(对齐方式):center、left、rightbottom3.2_选择器的使用3.3_TextVIEw的使用
多个布局方式以"|"分隔
边距
内边距:padding
外边距:margin
限制文本行数
androID: lines="2" androID:ellipsize="end"----超出的部分“。。。”表示
@H_499_301@放置图片(图片存储在mipmap(drawable)文件夹)
AndroID:drawable背景图片和颜色
AndroID:background="@mipmap/drawable_name"(背景图片)
AndroID:background=“颜色”
<TextVIEw androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:textSize="16sp" androID:textcolor="#ffffff" androID:padding="10dip" androID:background="#cc0000" androID:layout_gravity="center" androID:text="AndroID Studio 工具箱"/>
3.4_button按钮和点击事件的添加 button继承于TextVIEw
android背景选择器的使用
button点击事件的添加(最简单的方式,另外两种方式见3.6)
java文件下:
MainActivity文件中
public voID login(VIEw vIEw){ //动作 }
res.layout文件夹下activity_main文件中
<button androID:layout_wIDth="90dp" androID:layout_height="70dp" androID:text="登录" androID:textSize="25dp" androID:onClick="login"> </button>
3.5_EditText的使用基本使用:
<EditText androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:hint="提示" androID:inputType="datetime" ></EditText>
其中inputType是输入类型,输入类型有密码、数字、文本、日期等
如何在后端获取编辑框中的内容:
在java文件中
EditText edit_text1 = findVIEwByID(R.ID.ID名); edit_text1.getText().toString;
3.6_单选框radio button和radio group的使用直接应用:RadioGroup radioGroup = findVIEwByID(R.ID.radiogroup); radioGroup.setonCheckedchangelistener(new RadioGroup.OnCheckedchangelistener() { @OverrIDe public voID onCheckedChanged(RadioGroup group, int checkedID) { switch (checkedID){ case R.ID.radiobutton_man:{ Log.i("radiobutton_man","男生"); break; } case R.ID.radiobutton_woman:{ Log.i("radiobutton_woman","女生"); break; } } } });
效果:
2. 继承接口:
public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedchangelistener { @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); RadioGroup radioGroup = findVIEwByID(R.ID.radiogroup_1); radioGroup.setonCheckedchangelistener(this);//this在此使用的多态的属性,表示继承的接口类而不是MainActivity类 } @OverrIDe//实现接口中的抽象类 public voID onCheckedChanged(RadioGroup group, int checkedID) { switch (checkedID){ case R.ID.radiobutton_man:{ Log.i("radiobutton_man","男生"); break; } case R.ID.radiobutton_woman:{ Log.i("radiobutton_woman","女生"); break; } } }}
效果:
<CheckBox androID:ID="@+ID/chaoxiong" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="超凶" ></CheckBox>
CheckBox checkBox = findVIEwByID(R.ID.chaoxiong);//获取控件 checkBox.setonCheckedchangelistener(new Compoundbutton.OnCheckedchangelistener() {//进行监听 @OverrIDe public voID onCheckedChanged(Compoundbutton buttonVIEw, boolean isChecked) { if (isChecked){//如果被选中 Log.i("OnCheckedChange",buttonVIEw.toString()+"被选中"); } } });
3.8_Toast提示的使用Toast.makeText(MainActivity.this,"被选中",Toast.LENGTH_LONG).show();
3.9_AlertDialog的使用3.9.1_AlertDialog的简单使用 <button androID:ID="@+ID/button_alterdialog" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="删除" androID:onClick="delete" />
public voID delete(VIEw v){ AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("确认").setIcon(R.mipmap.why).setMessage("确定删除吗?") .setPositivebutton("确认", new DialogInterface.OnClickListener() {//创建确认选项按钮及其点击事件 @OverrIDe public voID onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"删除成功",Toast.LENGTH_SHORT).show(); } }).setNegativebutton("取消", new DialogInterface.OnClickListener() {//创建取消选项按钮及其点击事件 @OverrIDe public voID onClick(DialogInterface dialog, int which) { } }); //下面两个千万不能忘记 AlertDialog dialog = builder.create();//创建 dialog.show();//显示 }
效果:
<button androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="选择小周周的午餐" androID:onClick="select" ></button>
public voID select(VIEw vIEw){ AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("馋的流口水了都").setIcon(R.mipmap.greedy).setSingleChoiceItems(new String[]{"奥尔良鸡腿", "菠萝", "冰激凌"}, 0, new DialogInterface.OnClickListener() { @OverrIDe public voID onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "中午吃"+which, Toast.LENGTH_SHORT).show(); } }); //下面两个千万不能忘记 AlertDialog dialog = builder.create();//创建 dialog.show();//显示 }
效果:
<button androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="打开自定义界面" androID:onClick="customVIEw"></button>
public voID customVIEw(VIEw vIEw){ AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("新窗口").setVIEw(R.layout.activity_new); //下面两个千万不能忘记 AlertDialog dialog = builder.create();//创建 dialog.show();//显示 };
效果:
@H_557_502@
3.10.1_SimpleAdapter的使用(略)3.10.2_ArrayAdapter的使用<ListVIEw androID:ID="@+ID/ListvIEw" androID:layout_wIDth="wrap_content" androID:layout_height="match_parent" ></ListVIEw>
ListVIEw ListVIEw = findVIEwByID(R.ID.ListvIEw); String[] name = {"菠萝","青皮桔","香蕉","西瓜"}; ArrayAdapter arrayAdapter = new ArrayAdapter(newActivity.this,androID.R.layout.simple_List_item_1,androID.R.ID.text1,name); ListVIEw.setAdapter(arrayAdapter);
3.10.3_BaseAdapter的使用(略)4_生命周期与intent的使用4.1_生命周期@OverrIDeprotected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); LogUtils.d(TAG,"onCreate ---> 创建时调用");} @OverrIDeprotected voID onRestart() { super.onRestart(); LogUtils.d(TAG,"onRestart ---> 重启时调用");} @OverrIDeprotected voID onStart() { super.onStart(); LogUtils.d(TAG,"onStart ---> 即将可见不可交互时调用");} @OverrIDeprotected voID onResume() { super.onResume(); LogUtils.d(TAG,"onResume ---> 可见可交互时调用");} @OverrIDeprotected voID onPause() { super.onPause(); LogUtils.d(TAG,"onPause ---> 即将暂停时调用");} @OverrIDeprotected voID onStop() { super.onStop(); LogUtils.d(TAG,"onStop ---> 即将停止不可见时调用");} @OverrIDeprotected voID onDestroy() { super.onDestroy(); LogUtils.d(TAG,"onDestroy ---> 即将销毁时调用");}
4.2_页面跳转AndroIDManifest.xml代码注意,若要显示某个页面,必须将其在此文件夹中声明
<?xml version="1.0" enCoding="utf-8"?><manifest xmlns:androID="http://schemas.androID.com/apk/res/androID" package="com.example.myapplication"> <application androID:allowBackup="true" androID:icon="@mipmap/angry" androID:label="@string/app_name" androID:roundIcon="@mipmap/ic_launcher_round" androID:supportsRtl="true" androID:theme="@style/Apptheme"> <activity androID:name=".MainActivity"> <intent-filter> <action androID:name="androID.intent.action.MAIN" /> <category androID:name="androID.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity androID:name=".newActivity"> </activity> </application></manifest>
public voID windowskip(VIEw vIEw){//按钮点击事件 Intent intent = new Intent(); intent.setClass(MainActivity.this,newActivity.class);//从前者跳往后者 startActivity(intent);//启动 }
4.3_intent跳转页面传值MainActivity文件:
public voID loginSkip(VIEw vIEw){ Intent intent = new Intent(); intent.setClass(MainActivity.this,LoginActivity.class);//从前者跳往后者 intent.putExtra("name","jiajia"); intent.putExtra("sex","男"); intent.putExtra("hobby",new String[] {"篮球","乒乓球","羽毛球"}); startActivity(intent);//启动 }}
LoginActivity文件:
public class LoginActivity extends AppCompatActivity { protected voID onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_login); //接收传值 Intent intent = getIntent(); String name = intent.getStringExtra("name"); String sex = intent.getStringExtra("sex"); String[] hobbIEs = intent.getStringArrayExtra("hobby"); Log.i("LoginActivity",name+sex+hobbIEs);//打印 }}
4.4_intent传值自定义类只要继承了parcelable或者serializable接口就可以进行传值,注意接收的时候需要强行转成此自定义类的类型
4.5_回传数据(略)回传数据
总结以上是内存溢出为你收集整理的AndroidStudio使用入门全部内容,希望文章能够帮你解决AndroidStudio使用入门所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)