创建一个新项目
- 打开studio64.exe
- 打开雷电模拟器
- 新建项目,两种方式
1、启动Android Studio, 选择“Start a new Android Studio project”菜单。 然后在对话框中输入应用程序名称(Work1)、包名等参数。并选择Android SDK的版本。
2、File-New-New Project
选择空白模板
项目命名、项目位置、使用语言
- 完成项目
新建一个activity
修改主界面
在manifests中,修改主界面,将某个activity设为主界面
新建一个布局并引用
- 新建布局
右击layout-new-layout resource file
-命名布局
文件名:小写英文字母、数字和下划线组成,并且只能以小写字母开头,然后选择布局类型。
命名为main-ok
- 资源中修改引用布局
引用格式R.layout.布局名字
把activity_main改为新建的main,并删除activity_main.xml
app模块下的文件目录结构
manifests: 项目的配置信息文件。清单文件
java: 源代码和测试代码。
res: 资源目录,存储项目资源。
drawable目录存放图标资源。
layout目录存放用户界面布局文件。
values目录存放参数描述文件资源,都是XML文件,如字符
串 string.xml、颜色color.xml、 数组arrays.xml等。
res 下的常用资源目录
AndroidManifest.xml文件代码解释
- res资源引用方法:
程序中引用资源时,需要使用R类,其引用形式为:R.资源类型.资源名称
例如 在Activity中显示布局视图 setContentView(R.layout.main);
例如 Java程序要获得布局文件中的图片组件 img = (Image)findViewById(R.id.img);
Activity主程序介绍
widget包中的常用组件
修改快捷键
File-Settings,输入basic,修改快捷键,保存
常用文本框、编辑框、按钮的使用
main.xml的Design添加控件方法如图
在main.xml的Text添加控件,也可以修改控件相关的属性
刚刚添加的代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
一些常见性质
控件方向:android:orientation=“vertical"行,设置线性布局为垂直方向 /horizontal水平方向
控件宽度:android:layout_width=“match_parent"其中wrap_content/match_parent/dp(适应控件大小/填充到上一层容器的大小/200dp大小)
控件高度:android:layout_height=“wrap_content"适应大小
内容位置:android:layout_gravity=“center"控件内容的对齐方向,center(居中)
权重比:android:layout_weight = “1”
上边距:android:layout_marginTop=”14dp”
四边距:android:padding=“20sp”
控件背景:android:background = “@drawable/图片名”添加图片到drawable下,也可以是颜色
文本大小:android:textSize=“25sp”
文本颜色:android:textColor=”#8C6931"通过colors.xml资源来引用,也可直接写#FF0000红色
文本字体:android:textStyle=“bold|italic”字体风格normal/bold/italic(无效果/加粗/斜体)
文本类型:android:inputType=“textPassword"text密码文本
文本内容:android:text=“文本”
android:text=”@string/pass"使用键值对密码一般把字符串写到string.xml资源中,通过@String/xxx引用对应的字符串内容,也可以直接写
编辑框提示:android:hint=“请输入密码”
控件的Id:android:id=”@+id/textView"后续可以通过findViewById()的方法关联控件
控件重力:android:gravity = “bottom” 掉到底部
使用图片:app:srcCompat=”@drawable/photo”
使用数组:android:entries=”@array/sxiao”
修改图标:android:icon=”@drawable/图片名"
修改label:android:label=“文本内容”
在android.graphics.Color中定义了12种常见的颜色常数
Color.BLACK 黑色
Color.BLUE 蓝色
Color.CYAN 青绿色
Color.DKGRAY 灰黑色
Color.GRAY 灰色
Color.GREEN 绿色
Color.LTGRAY 浅灰色
Color.MAGENTA 红紫色
Color.RED 红色
Color.TRANSPARENT透明
Color.WHITE 白色
Color.YELLOW 黄色
线性布局LinearLayout
线性布局LinearLayout将组件按照水平或垂直方向排列。
1) 设置线性布局为水平方向 android:orientation = "horizontal” 一列一列的布局
2) 设置线性布局为垂直方向 android:orientation = “vertical” 一行一行的布局
一些常见使用
文本框TextView
编辑框EditText
按钮Button
按照1、定义2、关联3、事件来使用
public class MainActivity extends AppCompatActivity {
TextView textView;//1定义
EditText editText;
Button button;
String E1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView=findViewById(R.id.textView);//2关联
editText=findViewById(R.id.editText);
button=findViewById(R.id.button);
textView.setText("我饿了");//设置文本内容
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {//3事件使用
E1=editText.getText().toString();//获取文本内容
textView.setText(E1);
}
});
}
}
效果
点击按钮将编辑框上的内容传递给文本框
文本框TextView设置文本内容
textView.setText(内容);括号内为String,可以int类型+""转换为String
编辑框EditText获取文本内容
editText.getText().toString();获取编辑框内容,内容类型转换为String
按钮Button控件使用
关联控件(如按钮)、设置控件的事件监听、在监听接口添加事件处理程序
//1、 定义对象(变量)
Button b1;
//2、 关联控件 findViewById
e1 = findViewById(R.id.e1); b1 = findViewById(R.id.b1);
//3、 设置监听事件退出finish();
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String E1;//局部变量
E1 = e1.getText().toString();
//Toast提示框
Toast.makeText(getApplicationContext(),"用户名:"+E1,Toast.LENGTH_LONG).show();
}
});
控件使用步骤总结:
1、res-layout-main.xml添加相应控件
<Button
android:id="@+id/bt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/go" />
2、Java-com.example.mytestwork-MainActivity添加对应程序
Button bt;//定义
bt=findViewById(R.id.bt);//关联
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//String txt = et.getText().toString();
tv.setText(et.getText());
}
});
Toast提示框
三个参数(上下文-你所在的activity)(提示内容)(显示时间长短)
Toast.makeText(getApplicationContext(),"用户名:"+E1,Toast.LENGTH_LONG).show();
其他按钮
- 单选按钮:RadioButton 单选框:RadioGroup
if(radioButton1.isChecked()){
}else if(radioButton2.isChecked()){
}else{}
- 开关按钮:ToggleButton
android:textOff=“女”(关)
android:textOn=“男” (开)
android:checked=“true” (默认开)
if(toggleButton.isChecked()){
regX += “性别:”+to.getTextOn().toString()+"\n";
}else{
regX += “性别:”+to.getTextOff().toString()+"\n";}
修改图标及名称
在manifests下xml中修改图标android:icon="@mipmap/ic_launcher"
添加图片到drawable,修改背景
复制图片到drawable下,在文本中引用android:background = "@drawable/图片名”
其它
- 图片:ImageView
使用图片:app:srcCompat="@drawable/photo"
设置图片:imageView.setImageResource(R.drawable.photo); - 下拉框:Spinner
使用数组:android:entries="@array/sxiao"
获取下拉选项的id: Sl.getSelectedItemId() - 定义数组:
String[] ite = new String[]{1,2,3};
int[] ima = new int[]{R.drawable.shu,R.drawable.niu, R.drawable.hu};
在键值对里面定义数组:
<string-array name="sxiao">
<item>鼠</item>
<item>猪</item>
</string-array>
Activity周期
Activity有四种状态:运行状态、暂停状态、停止状态、销毁状态
七个周期
程序调试 LogCat
使用日志,查看进度Log.i(“lifeapplication”,“onStop”);
总结
大部分是来自之前学习时候做的笔记,很多东西不记下来几天就忘了
现在就是想到啥写的啥,有点乱,有机会再修改完善
都是一些Android studio的基本使用
买了书打算从头再看原理
欢迎指错,一起学习