package com.example.app1.fragment;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import com.example.app1.MainActivity;
import com.example.app1.R;
import org.greenrobot.eventbus.EventBus;
public class OneFragment extends Fragment {
private EditText editText;
public OneFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.fragment_one, container, false);
editText = inflate.findViewById(R.id.et);
Button button = inflate.findViewById(R.id.bt);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EventBus.getDefault().post(editText.getText().toString());
MainActivity.vp.setCurrentItem(1);
}
});
return inflate;
}
}
package com.example.app1.fragment;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import com.example.app1.R;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
public class TwoFragment extends Fragment {
private EditText editText;
private Button button;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EventBus.getDefault().register(this);
}
public TwoFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.fragment_two, container, false);
editText = inflate.findViewById(R.id.et);
button = inflate.findViewById(R.id.bt);
return inflate;
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void Aa(String a){
editText.setText(a);
}
@Override
public void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
}
package com.example.app1;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import com.example.app1.fragment.OneFragment;
import com.example.app1.fragment.TwoFragment;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
public static ViewPager vp;
private List<Fragment> list = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
OneFragment oneFragment = new OneFragment();
TwoFragment twoFragment = new TwoFragment();
list.add(oneFragment);
list.add(twoFragment);
vp.setAdapter(new Myadapter(getSupportFragmentManager(),list));
}
private void initView() {
vp = (ViewPager) findViewById(R.id.vp);
}
}
package com.example.app1;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import java.util.List;
public class Myadapter extends FragmentStatePagerAdapter {
private List<Fragment> list;
public Myadapter(@NonNull FragmentManager fm, List<Fragment> list) {
super(fm);
this.list = list;
}
@NonNull
@Override
public Fragment getItem(int position) {
return list.get(position);
}
@Override
public int getCount() {
return list.size();
}
}
package com.example.zhoukao1111;
import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import com.example.zhoukao1111.fragment.LoginFragment;
import com.example.zhoukao1111.fragment.RegisterFragment;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ViewPager vp;
private List<Fragment> list = new ArrayList<>();
private EditText etUser;
private EditText etPwd;
private Button loginBt;
private Button btton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
final SharedPreferences duoduo = getSharedPreferences("Duoduo", MODE_PRIVATE);
boolean flag = duoduo.getBoolean("flag", false);
if (flag) {
loginBt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final String user1 = etUser.getText().toString();
final String pwd1 = etPwd.getText().toString();
String user = duoduo.getString("user", null);
String pwd = duoduo.getString("pwd",null);
if (user1.equals(user)) {
if ( pwd1.equals(pwd)){
Toast.makeText(MainActivity.this, "用户名密码正确", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this, Main3Activity.class);
long timeMillis = System.currentTimeMillis();
SharedPreferences time = getSharedPreferences("Time", MODE_PRIVATE);
SharedPreferences.Editor edit = time.edit();
edit.putLong("Tim",timeMillis);
edit.commit();
Toast.makeText(MainActivity.this, "存入成功", Toast.LENGTH_SHORT).show();
startActivity(intent);
} else {
Toast.makeText(MainActivity.this, "用户名密码不正确", Toast.LENGTH_SHORT).show();
final Dialog dialog = new Dialog(MainActivity.this);
View inflate = dialog.getLayoutInflater().inflate(R.layout.winitem, null);
Button button = inflate.findViewById(R.id.bt_item);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
etUser.setText("");
etPwd.setText("");
dialog.hide();
Toast.makeText(MainActivity.this, "请重新输入用户名密码", Toast.LENGTH_SHORT).show();
}
});
dialog.setContentView(inflate);
dialog.create();
dialog.show();
}
} else {
Toast.makeText(MainActivity.this, "用户名不正确", Toast.LENGTH_SHORT).show();
}
}
});
}
final LoginFragment loginFragment = new LoginFragment();
RegisterFragment registerFragment = new RegisterFragment();
list.add(loginFragment);
list.add(registerFragment);
vp.setAdapter(new LoginAdapter(getSupportFragmentManager(), list));
btton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "请先注册后再登录", Toast.LENGTH_SHORT).show();
String user = etUser.getText().toString();
String pwd = etPwd.getText().toString();
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
intent.putExtra("user", user);
intent.putExtra("pwd", pwd);
startActivity(intent);
}
});
}
private void initView() {
vp = (ViewPager) findViewById(R.id.vp);
etUser = (EditText) findViewById(R.id.et_user);
etPwd = (EditText) findViewById(R.id.et_pwd);
loginBt = (Button) findViewById(R.id.login_bt);
btton = (Button) findViewById(R.id.btton);
}
}
package com.example.zhoukao1111;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import com.example.zhoukao1111.fragment.LoginFragment;
import com.example.zhoukao1111.fragment.RegisterFragment;
import java.util.ArrayList;
import java.util.List;
public class Main2Activity extends AppCompatActivity {
private ViewPager vp;
private EditText etUser;
private EditText etPwd;
private Button registerBt;
private List<Fragment> list = new ArrayList<>();
private String str = "[a-z]{6,15}";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
initView();
LoginFragment loginFragment = new LoginFragment();
RegisterFragment registerFragment = new RegisterFragment();
list.add(loginFragment);
list.add(registerFragment);
vp.setAdapter(new LoginAdapter(getSupportFragmentManager(),list));
Intent intent = getIntent();
String user = intent.getStringExtra("user");
String pwd = intent.getStringExtra("pwd");
etUser.setText(user+"");
etPwd.setText(pwd+"");
registerBt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final String user_ = etUser.getText().toString();
final String pwd_ = etPwd.getText().toString();
boolean user__ = user_.matches(str);
boolean pwd__ = pwd_.matches(str);
if (!user_.equals("")){
if (user__){
if (!pwd_.equals("")){
if (pwd__){
SharedPreferences duoduo = getSharedPreferences("Duoduo", MODE_PRIVATE);
SharedPreferences.Editor edit = duoduo.edit();
edit.putBoolean("flag",true);
edit.putString("user",user_);
edit.putString("pwd",pwd_);
edit.commit();
Toast.makeText(Main2Activity.this, "存入sp中", Toast.LENGTH_SHORT).show();
Intent intent1 = new Intent(Main2Activity.this,MainActivity.class);
startActivity(intent1);
}else {
}
}else {
Toast.makeText(Main2Activity.this, "密码为空", Toast.LENGTH_SHORT).show();
}
}else {
final PopupWindow popupWindow = new PopupWindow(Main2Activity.this);
View view1 = getLayoutInflater().inflate(R.layout.winitem, null);
Button button = view1.findViewById(R.id.bt_item);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
etUser.setText("");
etPwd.setText("");
Toast.makeText(Main2Activity.this, "请重新输入", Toast.LENGTH_SHORT).show();
popupWindow.dismiss();
}
});
popupWindow.setContentView(view1);
popupWindow.setHeight(600);
popupWindow.setWidth(600);
popupWindow.setOutsideTouchable(true);
popupWindow.showAsDropDown(registerBt,10,10);
}
}else {
Toast.makeText(Main2Activity.this, "用户名为空", Toast.LENGTH_SHORT).show();
}
}
});
}
private void initView() {
vp = (ViewPager) findViewById(R.id.vp);
etUser = (EditText) findViewById(R.id.et_user);
etPwd = (EditText) findViewById(R.id.et_pwd);
registerBt = (Button) findViewById(R.id.register_bt);
}
}
package com.example.zhoukao1111;
import android.Manifest;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
import com.bumptech.glide.Glide;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class Main3Activity extends AppCompatActivity {
private ImageView im;
private Button bt1;
private Button bt2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.INTERNET,Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
initView();
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String storageState = Environment.getExternalStorageState();
Glide.with(Main3Activity.this).load("/storage/emulated/0/duo.jpeg").into(im);
}
});
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new Thread(new Runnable() {
@Override
public void run() {
try {
URL url = new URL("http://b-ssl.duitang.com/uploads/blog/201402/13/20140213170837_dKAQC.jpeg");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
File file = Environment.getExternalStorageDirectory();
FileOutputStream fileOutputStream = new FileOutputStream(new File(file, "duo.jpeg"));
InputStream is = connection.getInputStream();
byte[] b = new byte[1024];
int len;
while ((len = is.read(b)) != -1) {
fileOutputStream.write(b, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
});
}
private void initView() {
im = (ImageView) findViewById(R.id.im);
bt1 = (Button) findViewById(R.id.bt1);
bt2 = (Button) findViewById(R.id.bt2);
}
}