Android 第一天

组件

  1. 组件

    1. 所有的组件继承 View
    2. by id寻找组件 findViewById(R.id.button);
    3. 组件绑定事件
      •  Button button = findViewById(R.id.button);
         button.setOnClickListener(new View.OnClickListener() {
             @Override public void onClick(View view) {
                      
              }
        });

         

    1. TextView 文本
    2. EditText 输入框
    3. Button 按钮
  2. 组件的属性

    1. width -->   android:layout_width="match_parent"  <父组件的宽度>
    2. height -->  android:layout_height="wrap_content" <当前组件的高度>
    3. content -->  android:text="@string/mobile"/>
    4. id --> android:id="@+id/button"

拨号

  1. 意图类 Intent --> 
    new Intent(Intent.ACTION_CALL,Uri.parse("tel:" + number));

     

  2. startActivity(intent);
  3. 申请权限 (AndroidManifest.xml) -- > 
    <uses-permission android:name="android.permission.CALL_PHONE" tools:ignore="ManifestOrder"/>

intent

  1. 跳转activity
 Intent toSecondActivity  = new Intent(MainActivity.this,SecondActivity.class);
 startActivity(toSecondActivity);

fragment

  1. 动态添加
BlankFragment firstFragment = new BlankFragment();
        firstFragment.setArguments(getIntent().getExtras());
        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.minaActivity, firstFragment).commit();

store

  1. SharedPreferences

    1. 获取sharedPreferences 对象
      1. SharedPreferences sp = getSharedPreferences("mrsoft", MODE_PRIVATE);
        1. 第一个参数String name -> 用来指定SharedPreferences的文件名
        2. 第二个int mode -> 访问权限
          1. MODE_PRIVATE 被本应用读写
          2. MODE_MULTI_PROCESS 跨应用读写
      2. getPreferences(int mode)
        1. 参数同上
    2. 存储
      final SharedPreferences sp = getSharedPreferences("mrsoft", MODE_PRIVATE); //获取SharedPreferences对象
              SharedPreferences.Editor edit = sp.edit(); //获取Editor对象
              //存放数据
              edit.putString("username","username");
              edit.putString("password","password");
              edit.commit();
  2. 内部存储

    1. 文件输出

      1. EditText text =  findViewById(R.id.username);
                    FileOutputStream fos = openFileOutput("memo",MODE_PRIVATE); //获取内部内存
                    fos.write(text.getText().toString().getBytes()); //存放数据
                    fos.flush(); //清楚缓存
                    fos.close();

         

    2. 输入

       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));
    3. 读取权限

      /manifests/AndroidManifest.xml  
      <manifest> 
          <!--外部内存读取权限-->
          <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
          <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

       

  3. 外部存储

    1. 可以在手机文件管理器访问到

    2. 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();

       

    3. 读取 -> 

       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

 

  1. Toast
    Toast.makeText(MainActivity.this, "账号数据库没有找到"+text.getText().toString(), Toast.LENGTH_SHORT).show();
                    

     

 

单词

1. vertical 直男我特抠 

2. horizontal 猴子日人在地平线

Layout

  1. RelativeLayout(游戏的登录界面可以用到)(已弃用)

  2. LinearLayout(qq 微信 其他最常用的Layout)

    1. 属性
      1. android:orientation="horizontal" 方向
      2. android:gravity="bottom|right" 
    2. 子属性
      1. android:layout_weight="2" 和flex 类似
  3. FrameLayout(表盘可以用到)
  4. 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>

     

  5. GridLayout

    1. 属性
      android:columnCount="3"  <!--列数-->
          android:orientation="vertical" <!--垂直-->
          android:rowCount="3" <!--行数-->

       

    2. 子属性
      1.  <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" //外边距
                />

         

    3. 1

 

 

权限

网络权限

 <uses-permission android:name="android.permission.INTERNET"
        tools:remove="android:maxSdkVersion"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值