第七天

EventBus

主界面

package com.example.day9.EventBus;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

import com.example.day9.R;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

public class Main3Activity extends AppCompatActivity {

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

        EventBus.getDefault().register(this);
    }

    @Subscribe
    public void getMessage( String str){
        Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}

发送数据的Fragment

package com.example.day9.EventBus;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.day9.R;

import org.greenrobot.eventbus.EventBus;

/**
 * A simple {@link Fragment} subclass.
 */
public class MyFragment extends Fragment {


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


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_my, container, false);
        inflate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post("你好!");
            }
        });
        return inflate;
    }

}

主界面

package com.example.day9.afternoon;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.example.day9.R;

public class Afternoon extends AppCompatActivity {

    private Button apButtonRead;
    private Button apButtonWrite;

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

        apButtonRead = (Button) findViewById(R.id.ap_button_read);
        apButtonWrite = (Button) findViewById(R.id.ap_button_write);

    }

    public void click(View view) {
        switch( view.getId()){
            case R.id.ap_button_read:
                SharedPreferences sp = getSharedPreferences("one", Context.MODE_PRIVATE);
                String key = sp.getString("key", "空。");
                Toast.makeText(this, key, Toast.LENGTH_SHORT).show();
                break;
            case R.id.ap_button_write:
                SharedPreferences one = getSharedPreferences("one", Context.MODE_PRIVATE);
                SharedPreferences.Editor edit = one.edit();
                edit.putString("key","values");
                edit.apply();
                break;
        }
    }
}

引导页

package com.example.day9.afternoon;

import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.day9.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 引导页
 */
public class GuidePage extends AppCompatActivity {
    private TextView gpText;
    private ViewPager gpViewPager;
    private List<Fragment> list;
    private ImageView gpImage1;
    private ImageView gpImage2;
    private ImageView gpImage3;

    private int index;

    private Handler han=new Handler();

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

        gpText = (TextView) findViewById(R.id.gp_text);
        gpViewPager = (ViewPager) findViewById(R.id.gp_viewPager);
        gpImage1 = (ImageView) findViewById(R.id.gp_image1);
        gpImage2 = (ImageView) findViewById(R.id.gp_image2);
        gpImage3 = (ImageView) findViewById(R.id.gp_image3);


        list=new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            GuidePageFragment gpf = new GuidePageFragment();
            list.add(gpf);
        }

        gpViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return list.get(i);
            }

            @Override
            public int getCount() {
                return list.size();
            }
        });

        gpViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                switch(i){
                    case 0:
                        gpImage1.setImageResource(R.drawable.radiuss);
                        gpImage2.setImageResource(R.drawable.dadiuss_false);
                        gpImage3.setImageResource(R.drawable.dadiuss_false);
                        break;
                    case 1:
                        gpImage1.setImageResource(R.drawable.dadiuss_false);
                        gpImage2.setImageResource(R.drawable.radiuss);
                        gpImage3.setImageResource(R.drawable.dadiuss_false);
                        break;
                    case 2:
                        Fragment fragment = list.get(2);
                        View view = fragment.getView();
                        Button viewById = view.findViewById(R.id.epf_button_click);
                        viewById.setVisibility(View.VISIBLE);
                        gpImage1.setImageResource(R.drawable.dadiuss_false);
                        gpImage2.setImageResource(R.drawable.dadiuss_false);
                        gpImage3.setImageResource(R.drawable.radiuss);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                han.post(new Runnable() {
                    @Override
                    public void run() {
                        if( index<list.size()){
                            Fragment fragment = list.get(index);
                            View view = fragment.getView();
                            if( view!=null) {
                                TextView text = view.findViewById(R.id.epf_text_time);
                                text.setText("倒计时" + (3 - index) + "秒。");
                            }
                            gpViewPager.setCurrentItem(index);
                            index++;
                        }else{
                            index=0;
                            timer.cancel();
                        }
                    }
                });

            }
        },0,1500);

    }

    //图片添加点击事件
    public void MyClick (View v) {
            switch( v.getId()){
                case R.id.gp_image1:
                    gpViewPager.setCurrentItem(0);
                    gpImage1.setImageResource(R.drawable.radiuss);
                    gpImage2.setImageResource(R.drawable.dadiuss_false);
                    gpImage3.setImageResource(R.drawable.dadiuss_false);
                    Toast.makeText(GuidePage.this, "点击了。", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.gp_image2:
                    gpViewPager.setCurrentItem(1);
                    gpImage2.setImageResource(R.drawable.radiuss);
                    gpImage1.setImageResource(R.drawable.dadiuss_false);
                    gpImage3.setImageResource(R.drawable.dadiuss_false);
                    break;
                case R.id.gp_image3:
                    gpViewPager.setCurrentItem(2);
                    gpImage3.setImageResource(R.drawable.radiuss);
                    gpImage1.setImageResource(R.drawable.dadiuss_false);
                    gpImage2.setImageResource(R.drawable.dadiuss_false);
                    break;

        }
    }

}

fragment java代码

package com.example.day9.afternoon;


import android.app.usage.UsageEvents;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.day9.R;

import org.greenrobot.eventbus.EventBus;

/**
 * A simple {@link Fragment} subclass.
 * 引导图
 */
public class GuidePageFragment extends Fragment {


    public GuidePageFragment() {
        // Required empty public constructor
    }
    private TextView epfTextTime;
    private ImageView pgfImage;
    private Button epfButtonClick;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_guide_page, container, false);

        epfTextTime = (TextView) inflate.findViewById(R.id.epf_text_time);
        pgfImage = (ImageView) inflate.findViewById(R.id.pgf_image);
        epfButtonClick = (Button) inflate.findViewById(R.id.epf_button_click);
        epfButtonClick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), RememberActivity.class);
                startActivity(intent);
            }
        });

        return inflate;
    }

}
fragment 布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".afternoon.GuidePageFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="倒计时3秒"
        android:textSize="25sp"
        android:textColor="#5A0421"
        android:fontFamily="sans-serif-condensed-light"
        android:id="@+id/epf_text_time"
        />
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:layout_centerVertical="true"
        android:id="@+id/pgf_image"
        />
    <Button
        android:layout_width="124dp"
        android:layout_height="76dp"
        android:text="点击退出"
        android:visibility="invisible"
        android:layout_centerInParent="true"
        android:background="#618"
        android:id="@+id/epf_button_click"
        />

</RelativeLayout>

记住密码的界面

package com.example.day9.afternoon;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.example.day9.R;

public class RememberActivity extends AppCompatActivity {

    private EditText ap2Id;
    private EditText ap2Pwd;
    private CheckBox ap2CheckBox;
    private Button ap2Button;

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

        ap2Id = (EditText) findViewById(R.id.ap2_id);
        ap2Pwd = (EditText) findViewById(R.id.ap2_pwd);
        ap2CheckBox = (CheckBox) findViewById(R.id.ap2_checkBox);
        ap2Button = (Button) findViewById(R.id.ap2_button);

        SharedPreferences sp = getSharedPreferences("user", Context.MODE_PRIVATE);
        boolean key = sp.getBoolean("key", false);
        if( key!=false){
            ap2Id.setText(sp.getString("id","admin"));
            ap2Pwd.setText(sp.getString("pwd","admin"));
            ap2CheckBox.setChecked(true);
        }
    }

    public void click(View view) {

        String id = ap2Id.getText().toString().trim();
        String pwd = ap2Pwd.getText().toString().trim();
        SharedPreferences user = getSharedPreferences("user", Context.MODE_PRIVATE);
        String sp_id = user.getString("id", "null");
        String sp_pwd = user.getString("pwd", "null");
        if( id.equals("admin")&&pwd.equals("admin")){
            if( ap2CheckBox.isChecked()){
                SharedPreferences.Editor edit = user.edit();
                edit.putBoolean("key",true);
                edit.putString("id",id);
                edit.putString("pwd",pwd);
                edit.apply();
            }
            Toast.makeText(this, "登陆成功。", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(this, "登录失败。", Toast.LENGTH_SHORT).show();
        }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值