2019-09-15安卓第一課

1.目錄

1.1layout存放編寫頁面樣式的xml文件

1.2drawable存放圖片的文件

1.3mipmap存放圖標文件

1.4value存放字符串和樣式文件

1.5java存放編寫功能的java文件

2.去除頂部導航欄

2.1編寫樣式

<style name="NoActionBar" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>

        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
<!--        設(shè)置工具欄的顏色-->
        <item name="colorControlNormal">@android:color/white</item>
    </style>

2.2 在AndroidManifest文件中引入樣式

< android:theme="@style/NoActionBar">

3 在AnddroidManifest中設(shè)置主頁面

 <activity android:name=".SplashActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

4線性布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:background="@drawable/bgimage"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    tools:context=".Forget_pwd_Activity">

    <include layout="@layout/tool_bar"/>
    <TextView
        android:id="@+id/your_username"
        android:layout_width="300sp"
        android:layout_height="wrap_content"
        android:text="你的用戶名是:"
        android:layout_marginBottom="8sp"
        android:layout_marginTop="20sp"
        android:textColor="#8a8a8a"
        android:textSize="20sp"
        android:visibility="gone"/>

    <EditText
        android:id="@+id/input_your_username"
        android:layout_width="300sp"
        android:layout_height="40sp"
        android:hint="請輸入你的用戶名"
        android:layout_gravity="center"
        android:drawableLeft="@drawable/see_user_small"
        android:drawablePadding="8dp"
        android:background="@drawable/border_text"
        android:paddingLeft="10sp"
        android:maxLines="1"
        android:visibility="gone"/>

    <TextView
        android:layout_width="300sp"
        android:layout_height="wrap_content"
        android:text="你的姓名是:"
        android:layout_marginBottom="8sp"
        android:layout_marginTop="20sp"
        android:textColor="#8a8a8a"
        android:textSize="20sp" />

    <EditText
        android:id="@+id/your_name"
        android:layout_width="300sp"
        android:layout_height="40sp"
        android:layout_marginBottom="20sp"
        android:hint="請輸入你的姓名"
        android:layout_gravity="center"
        android:drawableLeft="@drawable/see_user_small"
        android:drawablePadding="8dp"
        android:background="@drawable/border_text"
        android:paddingLeft="10sp"
        android:maxLines="1" />


    <Button
        android:id="@+id/confirm_btn"
        android:layout_width="300sp"
        android:layout_height="40sp"
        android:background="#f3ad02"
        android:textSize="20sp"
        android:layout_gravity="center"
        android:textColor="@android:color/white"
        android:layout_marginBottom="8sp"
        android:text="保        存" />
</LinearLayout>

5保存和讀取密碼和用戶名

保存

  /**
     * 保存用戶信息
     * @param username
     * @param password
     */
    private void savePref(String username, String password) {
        SharedPreferences.Editor editor = getSharedPreferences("data",MODE_PRIVATE).edit();
//        editor.putString("username",username);
//        editor.putString("password",password);
//        editor.putString("sex",sex);
        editor.putString(username,password);
        editor.apply();
    }

讀取

SharedPreferences sp = getSharedPreferences("data",MODE_PRIVATE);
        String pwd = sp.getString(username,"");

6頁面之間的傳值跳轉(zhuǎn)和拿取數(shù)據(jù)

 String username = etUsername.getText().toString();
                            Intent intent = new Intent();
                            intent.putExtra("username",username);
                            setResult(RESULT_OK,intent);
                            finish();
        Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
        startActivityForResult(intent,1);
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == RESULT_OK && data!=null){
            String username = data.getStringExtra("username");
            lgUsername.setText(username);
//            lgUsername.setSelection(username.length());
        }
    }

7底部導航欄和碎片

底部導航欄樣式文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".Main1Activity">
    <include layout="@layout/tool_bar"/>
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/fragment_main"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <RadioGroup
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:layout_marginTop="5sp"
            android:background="#eaeaea"
            android:id="@+id/radio_main"
            android:orientation="horizontal">

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/rb_home"
                android:layout_weight="1"
                android:button="@null"
                android:drawableTop="@drawable/selector_home_drawable"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="10dp"
                android:gravity="center"
                android:text="課程"
                android:textSize="18sp"
                android:textColor="@drawable/selector_text"/>
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/rb_find"
                android:layout_weight="1"
                android:button="@null"
                android:drawableTop="@drawable/selector_find_drawable"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="10dp"
                android:gravity="center"
                android:text="習題"
                android:textSize="18sp"
                android:textColor="@drawable/selector_text"
                />
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/rb_web"
                android:layout_weight="1"
                android:button="@null"
                android:drawableTop="@drawable/selector_web_drawable"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="10dp"
                android:gravity="center"
                android:text="資訊"
                android:textSize="18sp"
                android:textColor="@drawable/selector_text"
                />
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/rb_my"
                android:layout_weight="1"
                android:button="@null"
                android:drawableTop="@drawable/selector_my_drawable"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="10dp"
                android:gravity="center"
                android:text="我的"
                android:textSize="18sp"
                android:checked="true"
                android:textColor="@drawable/selector_text"/>
        </RadioGroup>
    </LinearLayout>
</LinearLayout>

碎片

package com.example;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.LoginActivity;
import com.example.myapplication.R;
import com.example.myapplication.SettingActivity;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link MySettingFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MySettingFragment extends Fragment {
    private boolean isLogin;
    private Context mContext;
    private TextView tvUsername;
    private LinearLayout headLayout,historyLayout,settingLayout;
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
//    private static final String ARG_PARAM1 = "param1";
//    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
//    private String mParam1;
//    private String mParam2;


    public MySettingFragment() {
        // Required empty public constructor
    }

    // TODO: Rename and change types and number of parameters
    public static MySettingFragment newInstance() {
        MySettingFragment fragment = new MySettingFragment();
        //給fragment傳參的方法
//        Bundle args = new Bundle();
//        args.putString(ARG_PARAM1, param1);
//        args.putString(ARG_PARAM2, param2);
//        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //接收fragment參數(shù)
//        if (getArguments() != null) {
//            mParam1 = getArguments().getString(ARG_PARAM1);
//            mParam2 = getArguments().getString(ARG_PARAM2);
//        }
    }

    //初始化Fragment的xml界面上所有控件和數(shù)據(jù),相當于Acticity的onCreate()作用
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        //1獲取fragment的
        this.mContext = getContext();
        this.isLogin = checkLoginStatus();
        View view = inflater.inflate(R.layout.fragment_my_setting, container, false);
        headLayout = view.findViewById(R.id.ll_head);
        tvUsername = view.findViewById(R.id.click_login);
        setUsername(isLogin);
        historyLayout = view.findViewById(R.id.list_history);
        settingLayout = view.findViewById(R.id.list_setting);
        //3設(shè)置事件監(jiān)聽
        headLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isLogin){

                }else {
                    Intent intent = new Intent(mContext, LoginActivity.class);
                    startActivityForResult(intent,1);
                }
            }
        });
        historyLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isLogin){

                }else {
                    Toast.makeText(mContext,"請先登錄",Toast.LENGTH_SHORT).show();
                }
            }
        });
        settingLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isLogin){
                    Intent intent = new Intent(mContext, SettingActivity.class);
                    startActivityForResult(intent,2);

                }else {
                    Toast.makeText(mContext,"請先登錄",Toast.LENGTH_SHORT).show();
                }
            }
        });
        return view;

    }

    private void setUsername(boolean isLogin) {
        if (isLogin){
            tvUsername.setText(readLoginInfo());
        } else {
            tvUsername.setText("點擊登錄");
        }
    }

    //獲取登錄狀態(tài)
    private boolean checkLoginStatus(){
        SharedPreferences sp = mContext.getSharedPreferences("data",Context.MODE_PRIVATE);
        return sp.getBoolean("isLogin",false);
    }

    private String readLoginInfo() {
        SharedPreferences sp = mContext.getSharedPreferences("data",Context.MODE_PRIVATE);
        return sp.getString("loginUser","");
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == Activity.RESULT_OK && data!=null) {
            isLogin = data.getBooleanExtra("isLogin", false);
            setUsername(isLogin);
        } else if (requestCode == 2 && resultCode == Activity.RESULT_OK && data!=null){
            isLogin =data.getBooleanExtra("isLogin",false);
            setUsername(isLogin);
        }
    }
}

在drawable文件下建立選擇器

image.png

圖標選擇器(selector)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="false" android:drawable="@drawable/my"/>
    <item android:state_checked="true" android:drawable="@drawable/my_selected"/>
</selector>

文字選擇器

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="false" android:color="#8a8a8a"/>
    <item android:state_checked="true" android:color="#09bb07"/>
</selector>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末搜锰,一起剝皮案震驚了整個濱河市招拙,隨后出現(xiàn)的幾起案子就乓,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,386評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件戚啥,死亡現(xiàn)場離奇詭異,居然都是意外死亡锉试,警方通過查閱死者的電腦和手機猫十,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,142評論 3 394
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來呆盖,“玉大人拖云,你說我怎么就攤上這事∮τ郑” “怎么了宙项?”我有些...
    開封第一講書人閱讀 164,704評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長株扛。 經(jīng)常有香客問我尤筐,道長,這世上最難降的妖魔是什么洞就? 我笑而不...
    開封第一講書人閱讀 58,702評論 1 294
  • 正文 為了忘掉前任盆繁,我火速辦了婚禮,結(jié)果婚禮上旬蟋,老公的妹妹穿的比我還像新娘油昂。我一直安慰自己,他們只是感情好倾贰,可當我...
    茶點故事閱讀 67,716評論 6 392
  • 文/花漫 我一把揭開白布冕碟。 她就那樣靜靜地躺著,像睡著了一般匆浙。 火紅的嫁衣襯著肌膚如雪安寺。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,573評論 1 305
  • 那天首尼,我揣著相機與錄音挑庶,去河邊找鬼。 笑死饰恕,一個胖子當著我的面吹牛挠羔,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播埋嵌,決...
    沈念sama閱讀 40,314評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼俱恶!你這毒婦竟也來了雹嗦?” 一聲冷哼從身側(cè)響起范舀,我...
    開封第一講書人閱讀 39,230評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎了罪,沒想到半個月后锭环,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,680評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡泊藕,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,873評論 3 336
  • 正文 我和宋清朗相戀三年辅辩,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片娃圆。...
    茶點故事閱讀 39,991評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡玫锋,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出讼呢,到底是詐尸還是另有隱情撩鹿,我是刑警寧澤,帶...
    沈念sama閱讀 35,706評論 5 346
  • 正文 年R本政府宣布悦屏,位于F島的核電站节沦,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏础爬。R本人自食惡果不足惜甫贯,卻給世界環(huán)境...
    茶點故事閱讀 41,329評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望看蚜。 院中可真熱鬧获搏,春花似錦、人聲如沸失乾。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,910評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽碱茁。三九已至裸卫,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間纽竣,已是汗流浹背墓贿。 一陣腳步聲響...
    開封第一講書人閱讀 33,038評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留蜓氨,地道東北人聋袋。 一個月前我還...
    沈念sama閱讀 48,158評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像穴吹,于是被迫代替她去往敵國和親幽勒。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,941評論 2 355

推薦閱讀更多精彩內(nèi)容