1.按鈕四種點擊事件寫法
btn_test1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"我被點擊了",Toast.LENGTH_LONG).show();
}
});
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//1.找的按鈕
Button btn_test1 = (Button) findViewById(R.id.button1);
Button btn_test2 = (Button) findViewById(R.id.button2);
Button btn_test3 = (Button) findViewById(R.id.button3);
Button btn_test4 = (Button) findViewById(R.id.button4);
/* //2.通過查看 api文檔得知 可以通過匿名內部類去實現
btn_test1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"我被點擊了",Toast.LENGTH_LONG).show();
}
});*/
//3 當頁面上有多個按鈕需要點擊的時候 可以這樣寫
btn_test1.setOnClickListener(this);
btn_test2.setOnClickListener(this);
btn_test3.setOnClickListener(this);
btn_test4.setOnClickListener(this);
}
@Override
public void onClick(View v) {
//4.由於每個按鈕的id 不一樣 所以可以通過id來判斷用戶到底點擊的是哪個按鈕
switch (v.getId()){
case R.id.button1:
Toast.makeText(MainActivity.this,"我被點擊了111",Toast.LENGTH_LONG).show();
break;
case R.id.button2:
Toast.makeText(MainActivity.this,"我被點擊了222",Toast.LENGTH_LONG).show();
break;
case R.id.button3:
Toast.makeText(MainActivity.this,"我被點擊了333",Toast.LENGTH_LONG).show();
break;
case R.id.button4:
Toast.makeText(MainActivity.this,"我被點擊了444",Toast.LENGTH_LONG).show();
break;
}
}
}
<Button
android:text="test5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="16dp"
android:onClick="click"
android:id="@+id/button5" />
public void click(View v){
Toast.makeText(MainActivity.this,"我被點擊了555",Toast.LENGTH_LONG).show();
}
2.android中常見佈局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"//設置朝向,這個是垂直,另一個是水平
android:layout_height="match_parent">
<!--@符合就代表R文件-->
<TextView
android:id="@+id/tv_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="請輸入手機號:"
android:textSize="25sp"
/>
<EditText
android:id="@+id/et_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入手機號"//輸入前的輸入提示
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="撥打此號碼"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--快速格式化代碼 ctrl + alt + l-->
<!--@符合就代表R文件-->
<TextView
android:id="@+id/tv_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="請輸入手機號:"
android:textSize="25sp" />
<EditText
android:id="@+id/et_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tv_number"
android:hint="請輸入手機號"1
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/et_number"//_above代表處在誰上面。_torightof右面;_toleftof左面
android:text="撥打此號碼"
/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--@符合就代表R文件-->
<TextView
android:id="@+id/tv_number"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="這是視頻播放的內容:"
android:textSize="25sp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="撥打"
android:layout_gravity="center"//相對於父親居中;
center_vertical 水平居中//還有一個 android:gravity="center" 這個相對於控件本身居中/>
</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--一個tablerow就代表一行 具體的控件表示列-->
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1111" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2222" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1111" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2222" />
</TableRow>
</TableLayout>
3.android中單位
dp :一個基於density的抽象單位,如果一個160dpi的屏幕,1dp=1px
dip :等同於dp
sp :同dp相似,但還會根據用戶的字體大小偏好來縮放(建議使用sp作爲文本的單位,其它用dip)
4.android中單元測試
public class ExampleUnitTest {
@Test
public void testAdd(){
Calc calc = new Calc();
int add = calc.add(3, 5);
//判斷一下結果是真的還是假的 斷言: 參數1:期望值 參數2:實際值
assertEquals(8,add);
}
}
5.Log類的使用
System.out.println("aab");
//android開發中 谷歌單獨提供了打印日誌的類 Log 參數1:對log信息的標示 參2:就代表具體打印的日誌信息
Log.e("MainActivity","e級別");
Log.w("MainActivity","w級別");
Log.d("MainActivity","d級別");
Log.i("MainActivity","i級別");
Log.v("MainActivity","v級別");
6.登錄案例
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.itheima.login.MainActivity">
<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content" 後面加的:
android:inputType="textPassword"// 代表輸入格式爲文字,不可見android:hint="請輸入用戶名"
/>
<EditText
android:id="@+id/et_pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入密碼"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_marginTop="20dp"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/cb_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="記住用戶名密碼"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登錄"
android:layout_alignParentRight="true"//表示靠近父親的右邊
android:onClick="click"
/>
</RelativeLayout>
</LinearLayout>
public class UserUtils {
//用來存儲用戶密碼業務方法 ctrl + alt + t 抓異常
public static boolean save(String name, String pwd) {
try {
//1.爲了存儲方便 拼接字符串
String data = name + "##"+pwd;
//2.把data存到手機裏(不能存到 C ,D盤) ---->手機裏有內部存儲目錄() 和 外部存儲目錄
File file = new File("/data/data/com.itheima.login/info.txt");
//3.創建一個文件輸入流
FileOutputStream fos = new FileOutputStream(file);
//4.把data寫到文件
fos.write(data.getBytes());
//5.關流
fos.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
//讀取info.txt文件裏面的內容
public static Map<String,String> readInfo(){
try {
//1.創建一個map對象用來存儲 我們讀取出來的數據
Map<String,String> maps = new HashMap<>();
//2.去info.txt文件裏面讀
File file = new File("/data/data/com.itheima.login/info.txt");
//3.創建FileinputStream
FileInputStream fis = new FileInputStream(file);
//4.使用一個轉換流 可以快速讀取一行數據
BufferedReader bufr = new BufferedReader(new InputStreamReader(fis));
//5.讀一行 把結果數據讀取出來
String result = bufr.readLine(); // aa##123
//6.切割字符串 根據##
String[] split = result.split("##");
//7.獲取用戶名 aa
String name = split[0];
//8.獲取密碼
String pwd = split[1];
//9.把name 和 pwd存到集合裏
maps.put("name",name);
maps.put("pwd",pwd);
return maps;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
7.Context類(上下文)
8.把數據存儲到sd卡
String sdPath = Environment.getExternalStorageDirectory().getPath();
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){ }
//1.獲取sd卡空間
File file = Environment.getExternalStorageDirectory();
//2.在file類中提供了2個方法 可以獲取總空間 和 可用空間 谷歌已經幫助我們封裝了
long totalSpace = file.getTotalSpace();
long usableSpace = file.getUsableSpace();
//3.所以需要把值轉換爲對應的單位 參數1:上下文this, //TODO 明天講
String totalSpaceSize = Formatter.formatFileSize(this, totalSpace);
String usableSpaceSize = Formatter.formatFileSize(this, usableSpace);
System.out.println("total:"+totalSpaceSize);
System.out.println("usable:"+usableSpaceSize);
9.文件權限
//3.說明用戶名密碼不爲空 使用sp來存數據 獲取sp的實例
SharedPreferences sp = getSharedPreferences("config", 0); //:使用sp 系統會生成一個xml格式的文件
//3.1 獲取sp的編輯器
SharedPreferences.Editor edit = sp.edit();
//3.2 存數據 用戶名 和密碼
edit.putString("name",name);
edit.putString("pwd",pwd);
//3.3 調用commit方法提交數據
edit.commit();
1.撥打電話
public class MainActivity extends AppCompatActivity {
private List<Sms> lists;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);//這裏是獲取
btn_test1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"我被點擊了",Toast.LENGTH_LONG).show();
}
});
2.登錄案例(步驟看老師筆記,下面是詳細代碼)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.itheima.login.MainActivity">
<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入用戶名" />
<EditText
android:id="@+id/et_pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="請輸入密碼" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">
<CheckBox
android:id="@+id/cb_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="記住用戶名密碼" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:onClick="click"
android:text="登錄" />
</RelativeLayout>
</LinearLayout>
package com.itheima.login;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Map;
/**
* Author: 邱瓏
* Version: 1.0
* Date: 2017/8/21
* Modify:
* Description: //TODO
* Copyright notice:
*/
public class Test extends AppCompatActivity {
private TextView tx1;
private TextView tx2;
private CheckBox cb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
tx1 = (TextView) findViewById(R.id.textView1);
tx2 = (TextView) findViewById(R.id.textView2);
cb = (CheckBox) findViewById(R.id.checkBox);
Map<String,String> map = UserUtils.read();
if(map.equals(null)){
}else{
tx1.setText(map.get("name"));
tx2.setText(map.get("pwd"));
}
}
public void click(View view){
String name = tx1.getText().toString().trim();
String pwd = tx2.getText().toString().trim();
if(TextUtils.isEmpty(name)||TextUtils.isEmpty(pwd)){
Toast.makeText(this,"用戶名或密碼不能爲空",Toast.LENGTH_LONG).show();
}
else if(cb.isChecked()){
boolean a = UserUtils.save(this,name,pwd);
if(a){
Toast.makeText(this,"成功",Toast.LENGTH_LONG).show();
}else
Toast.makeText(this,"失敗",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(this,"沒有勾選保存信息",Toast.LENGTH_LONG).show();
}
}
}
package com.itheima.login;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map;
/**
* Created by jinxi on 2017/8/20.
*/
public class UserUtils {
public static boolean save(String name, String pwd) {
String num = name+"##"+pwd;
File f = new File("data/data/com.itheima.login/info.txt");
try {
FileOutputStream fos = new FileOutputStream(f);
fos.write(num.getBytes());
fos.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public static Map<String,String> read(){
Map<String,String> map = new HashMap();
File f = new File("data/data/com.itheima.login/info.txt");
try {
FileInputStream fis = new FileInputStream(f);
BufferedReader br = new BufferedReader(new FileReader(f));
String a =br.readLine();
String[] as =a.split("##");
map.put("name",as[0]);
map.put("pwd",as[1]);
return map;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
String path = context.getFilesDir().getPath();
File f = new File(path,"info21.txt");//後面是給文件命名
//File f = new File("data/data/com.itheima.login/info.txt");
// String savePath = context.getFilesDir().getPath(); // data/data/當前應用包名/files/
// File file = new File(savePath,"info2.txt");
// FileOutputStream fos = new FileOutputStream()
//3.創建一個文件輸入流 mode參數: 0:代表私有的模式
FileOutputStream fos = context.openFileOutput("info3.txt", 0); //info3.txt // data/data/當前應用包名/files/