Android(1)-----單擊按鈕更換背景,事件監聽,頁面切換Intent,intent消息傳遞

一、單擊按鈕更換背景java

 


 

二、基本佈局介紹android

 

android:textAlignment="center"//控件對齊
android:gravity="center"//文字對齊
android:orientation="horizontal"//LinearLayout 的水平排列
android:layout_gravity="center" //控件的居中
android:inputType="textPassword" //設置爲密碼框
android:src="@drawable/char1" //設置imageView 的圖片
 
 


例子:實現了水平並排和居中
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="其餘:"
android:layout_marginLeft="40dp"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:hint="請輸入"
android:layout_marginRight="40dp"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
 

 

三、事件監聽:sql

 


 

四、頁面切換Intent:數據庫

A、新建java.class:MainActivity2.class,相對應還要建XML文件app

在事件監聽器寫以下代碼:ide

Intent intent1=new Intent(MainActivity.this,MainActivity2.class);//參數: 當前頁面,要跳轉到的頁面的class文件
intent1.putExtra("name",editText_name.getText());//參數:key用於識別;要傳遞的字符串
startActivity(intent1);//注:和接收碼startActivityForResult()做用同樣。

B、在AndroidManifest對java.class進行註冊
<activity android:name=".MainActivity2"></activity>
C、接受消息
Intent intent2=getIntent();
name=intent2.getStringExtra("name");

五、請求碼和接受碼的使用
點擊跳轉的事件監聽中把startActivity(intent1)替換成:
startActivityForResult(intent1,4836);//接受碼
點擊跳轉回來的事件監聽
Intent intent2=new Intent(MainActivity2.this,MainActivity.class);
intent2.putStringArrayListExtra("arrayList_hobby",arrayList_hobby);
setResult(4837,intent2);//請求碼
finish();
 
 

 六、數據庫的使用:

6_一、繼承
extends SQLiteOpenHelper
6_二、重寫構造器
 
 
//用於建立數據庫
    private static final String DB_NAME="db_user"; private static final int DB_VERSION=1; public DatabaseHelper(Context context) { super(context, DB_NAME, null,DB_VERSION);//建立數據庫
    }
6_三、重寫兩個方法:
 @Override public void onCreate(SQLiteDatabase db) { } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { }

6_四、建立表結構:  要在SQL語句的鏈接 記得加上對應空格
佈局

//用於建立表和表結構
    private static final String TABLE_USER="users"; private static final String COL_ID="id"; private static final String COL_USERNAME="username"; private static final String COL_PWD="pwd";
//要在SQL語句的鏈接 記得加上對應空格
private static final String sql=" create table "+TABLE_USER+"( "+COL_ID+" integer primary key autoincrement, "+COL_USERNAME+" text, "+COL_PWD+" text "+")"; @Override public void onCreate(SQLiteDatabase db) { //建立表 Log.i("db","onCreate"); //String sql="create table"+TABLE_USER+"("+COL_ID+"integer primary key autoincrement,"+COL_USERNAME+"text,"+COL_PWD+"text"+")"; db.execSQL(sql); }

 6_5 增長數據,查詢數據字體

//插入數據
    public boolean add(Users users){ SQLiteDatabase db=this.getReadableDatabase(); ContentValues values=new ContentValues(); values.put(COL_USERNAME,users.getUserName()); values.put(COL_PWD,users.getPwd()); double flag=0; try { flag=db.insert(TABLE_USER,null,values); }catch (Exception e){ Log.i("DatabaseError", e.getMessage()); } if (flag>0)return true; return false; } //查詢數據1
    public ArrayList<Users> getAllDAata(){ ArrayList<Users> list=new ArrayList<>(); SQLiteDatabase db=this.getReadableDatabase(); //遊標
        Cursor cursor=null; cursor=db.rawQuery("select * from "+TABLE_USER,null); if(cursor!=null &&cursor.moveToFirst()){ do{ Users users=new Users(); users.setId(cursor.getInt(cursor.getColumnIndex(COL_ID)) ); users.setPwd(cursor.getString(cursor.getColumnIndex(COL_PWD)));//注意這裏是getString
                users.setUserName(cursor.getString(cursor.getColumnIndex(COL_USERNAME)));//獲得列名的索引,再根據索引獲得內容
 list.add(users); }while (cursor.moveToNext()); } if(cursor!=null){ cursor.close();//用來關閉遊標
 } return list; }

 

 

 


 

七、SharedPreferences()的使用 :用來保存用戶的設置
記得最後要進行應用,不該用用不了 editor.apply();//須要對editor進行應用
 

 

 



 
代碼的基本知識點:
editText_name.getText().length()==0 //用來判斷輸入框是否爲空
editText_name.getText().toString() //獲得輸入框的內容
if(register_userPwd.getText().toString().equals(register_againUserPwd.getText().toString()) ){//getText()進行toString才能使用equal()
 sideIndex.setBackgroundColor(Color.parseColor("#8a8a8a")); //設置字體顏色
相關文章
相關標籤/搜索