组件
-
组件
- 所有的组件继承 View
- by id寻找组件 findViewById(R.id.button);
- 组件绑定事件
-
Button button = findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { } });
-
-
- TextView 文本
- EditText 输入框
- Button 按钮
-
组件的属性
- width --> android:layout_width="match_parent" <父组件的宽度>
- height --> android:layout_height="wrap_content" <当前组件的高度>
- content --> android:text="@string/mobile"/>
- id --> android:id="@+id/button"
拨号
- 意图类 Intent -->
new Intent(Intent.ACTION_CALL,Uri.parse("tel:" + number));
-
startActivity(intent);
- 申请权限 (AndroidManifest.xml) -- >
<uses-permission android:name="android.permission.CALL_PHONE" tools:ignore="ManifestOrder"/>
intent
- 跳转activity
Intent toSecondActivity = new Intent(MainActivity.this,SecondActivity.class);
startActivity(toSecondActivity);
fragment
- 动态添加
BlankFragment firstFragment = new BlankFragment();
firstFragment.setArguments(getIntent().getExtras());
getSupportFragmentManager()
.beginTransaction()
.add(R.id.minaActivity, firstFragment).commit();
store
-
SharedPreferences
- 获取sharedPreferences 对象
-
SharedPreferences sp = getSharedPreferences("mrsoft", MODE_PRIVATE);
- 第一个参数String name -> 用来指定SharedPreferences的文件名
- 第二个int mode -> 访问权限
- MODE_PRIVATE 被本应用读写
- MODE_MULTI_PROCESS 跨应用读写
- getPreferences(int mode)
- 参数同上
-
- 存储
final SharedPreferences sp = getSharedPreferences("mrsoft", MODE_PRIVATE); //获取SharedPreferences对象 SharedPreferences.Editor edit = sp.edit(); //获取Editor对象 //存放数据 edit.putString("username","username"); edit.putString("password","password"); edit.commit();
- 获取sharedPreferences 对象
-
内部存储
-
文件输出
-
EditText text = findViewById(R.id.username); FileOutputStream fos = openFileOutput("memo",MODE_PRIVATE); //获取内部内存 fos.write(text.getText().toString().getBytes()); //存放数据 fos.flush(); //清楚缓存 fos.close();
-
-
输入
EditText text = findViewById(R.id.getUsername); FileInputStream fis = openFileInput("memo"); byte[] buffer = new byte[fis.available()]; fis.read(buffer); fis.close(); Toast.makeText(this, new String(buffer), Toast.LENGTH_SHORT).show(); text.setText(new String(buffer));
-
读取权限
/manifests/AndroidManifest.xml <manifest> <!--外部内存读取权限--> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
-
-
外部存储
-
可以在手机文件管理器访问到
-
EditText text = findViewById(R.id.username); File f = new File(Environment.getExternalStorageState(), "text.text"); FileOutputStream s = new FileOutputStream(f); s.write(text.getText().toString().getBytes()); s.flush(); s.close();
-
读取 ->
FileInputStream fis= new FileInputStream(new File(Environment.getExternalStorageState(), "text.text")); EditText text = findViewById(R.id.getUsername); byte[] buffer = new byte[fis.available()]; fis.read(buffer); fis.close(); Toast.makeText(this, new String(buffer), Toast.LENGTH_SHORT).show(); text.setText(new String(buffer));
-
other
- Toast
Toast.makeText(MainActivity.this, "账号数据库没有找到"+text.getText().toString(), Toast.LENGTH_SHORT).show();
单词
1. vertical 直男我特抠
2. horizontal 猴子日人在地平线
Layout
-
RelativeLayout(游戏的登录界面可以用到)(已弃用)
-
LinearLayout(qq 微信 其他最常用的Layout)
-
属性
- android:orientation="horizontal" 方向
-
android:gravity="bottom|right"
- 子属性
-
android:layout_weight="2" 和flex 类似
-
-
- FrameLayout(表盘可以用到)
-
TableLayout
<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:id="@+id/minaActivity" android:background="@mipmap/belle" android:paddingTop="@android:dimen/app_icon_size" android:stretchColumns="0,3" <!-- 允许被拉伸--> > <TableRow android:layout_marginTop="500dp"> <TextView/> <TextView android:layout_height="wrap_content" android:layout_weight="wrap_content" android:textSize="19dp" android:gravity="center_horizontal" android:text="账号" /> <EditText android:layout_weight="match_parent" android:layout_height="wrap_content" android:hint="邮箱/手机号?"/> <TextView/> </TableRow> <TableRow> <TextView/> <TextView android:layout_height="wrap_content" android:layout_weight="wrap_content" android:textSize="19dp" android:gravity="center_horizontal" android:text="账号" /> <EditText android:layout_weight="match_parent" android:layout_height="wrap_content" android:hint="输入17位数字或密码?"/> <TextView/> </TableRow> </TableLayout>
-
GridLayout
- 属性
android:columnCount="3" <!--列数--> android:orientation="vertical" <!--垂直--> android:rowCount="3" <!--行数-->
- 子属性
-
<ImageView android:src="@mipmap/a1" android:layout_column="1" //第几列 android:layout_row="0" //第几行 android:layout_gravity="right" //布局 android:layout_columnSpan="4" //占据行数 android:layout_marginRight="4dp" //外边距 />
-
- 1
- 属性
权限
网络权限
<uses-permission android:name="android.permission.INTERNET"
tools:remove="android:maxSdkVersion"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>