记得删除
private EditText ed01;
private EditText ed02;
private CheckBox pswd;
private CheckBox login;
private Button btn01;
private SharedPreferences mPreferences;
private String mPwd;
private String mLog;
private Presenter mPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {//初始化控件
ed01 = findViewById(R.id.ed01);
ed02 = findViewById(R.id.ed02);
pswd = findViewById(R.id.pswd);
login = findViewById(R.id.login);
btn01 = findViewById(R.id.btn01);
//获取一个保存数据的SharedPreferences
mPreferences = getSharedPreferences("1606B", Context.MODE_PRIVATE);
//设置数据
boolean boon = mPreferences.getBoolean("boon", false);
String pwd = mPreferences.getString("pwd", "");
String phone = mPreferences.getString("phone", "");
mPreferences.getString("admin", "");
//判读是否记住密码
if (boon){
ed01.setText(pwd);
ed02.setText(phone);
pswd.setChecked(true);
}
//判读是否自动登录
boolean boon1 = mPreferences.getBoolean("boon1", false);
if (boon1){
login.setChecked(true);
startActivity(new Intent(MainActivity.this,Main2Activity.class));
//finish();
}
btn01.setOnClickListener(this);
mPresenter = new Presenter();
}
private String req="http://www.xieast.com/api/user/login.php?username=13800138000&password=123456";
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn01:
boolean submit = submit();
if (submit){
return;
}
//Toast.makeText(MainActivity.this,""+lin,Toast.LENGTH_SHORT).show();
mPresenter.setQingq(req,mPwd,mLog);
mPresenter.setQiri(new Presenter.Qiri() {
@Override
public void seccess(String ss) {
if (pswd.isChecked()){
SharedPreferences.Editor edit = mPreferences.edit();
edit.putBoolean("boon",true);
edit.putString("pwd",mPwd);
edit.putString("phone",mLog);
edit.putString("admin",ss);
edit.commit();
}
if (login.isChecked()){
SharedPreferences.Editor edit = mPreferences.edit();
edit.putBoolean("boon1",true);
edit.commit();
}
startActivity(new Intent(MainActivity.this,Main2Activity.class));
//finish();
}
@Override
public void failure() {
Toast.makeText(MainActivity.this,"账号密码不正确",Toast.LENGTH_SHORT).show();
}
});
break;
}
}
private boolean submit() {
// validate
mPwd = ed01.getText().toString().trim();
mLog = ed02.getText().toString().trim();
if (TextUtils.isEmpty(mPwd) && TextUtils.isEmpty(mLog)) {
Toast.makeText(this, "账号密码不能为空", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
}
public class Main2Activity extends AppCompatActivity {
private ViewPager pager;
private RadioGroup group;
private List<Fragment> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
initView();
}
private void initView() {
pager = findViewById(R.id.pager);
group = findViewById(R.id.radio);
list = new ArrayList<>();
list.add(new Fragment01());
list.add(new Fragment02());
initData();
}
//初始化数据
private void initData() {
pager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int i) {
return list.get(i);
}
@Override
public int getCount() {
return list.size();
}
});
//滑动
pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int i) {
switch (i){
case 0:
group.check(R.id.r_btn01);
break;
case 1:
group.check(R.id.r_btn02);
break;
}
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
//点击
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (i){
case R.id.r_btn01:
pager.setCurrentItem(0);
break;
case R.id.r_btn02:
pager.setCurrentItem(1);
break;
}
}
});
}
}
public class Fragment01 extends Fragment {
private ListView lv;
private List<LeiBean.DataBean> mData;
private String req="http://www.xieast.com/api/news/news.php";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment01, container, false);
lv = view.findViewById(R.id.lv);
new HttpUrl().getHttpUrl(req).setWlqq(new HttpUrl.Wlqq() {
@Override
public void sueess(String s) {
LeiBean s1 = new Gson().fromJson(s, LeiBean.class);
mData = s1.getData();
lv.setAdapter(new MAdapter(mData,getLayoutInflater()));
}
});
return view;
}
}
public class Fragment02 extends Fragment implements View.OnClickListener {
private ImageView fr1_image;
private Button fr1_button1;
private SharedPreferences mSharedPreferences;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.itemaa, container, false);
mSharedPreferences = getActivity().getSharedPreferences("1606B", Context.MODE_PRIVATE);
initView(view);
return view;
}
private void initView(View view) {
fr1_image = view.findViewById(R.id.fr1_image);
fr1_button1 = view.findViewById(R.id.fr1_button1);
String admin = mSharedPreferences.getString("admin", "");
try{
Bitmap qrCode = EncodingUtils.createQRCode(admin, 500, 500, null);
fr1_image.setImageBitmap(qrCode);
}catch (Exception e){
}
fr1_button1.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.fr1_button1:
getActivity().finish();
break;
}
}
写
public interface Wlqq{
void sueess(String s);
}
private Wlqq mWlqq;
public void setWlqq(Wlqq wlqq) {
mWlqq = wlqq;
}
public HttpUrl getHttpUrl(String ss){
new HttpUrl.Sreq(){}.execute(ss);
return this;
}
class Sreq extends AsyncTask<String,Void,String> {
String mString=null;
@Override
protected String doInBackground(String... strings) {
try {
URL url = new URL(strings[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
int responseCode = connection.getResponseCode();
if (responseCode==200){
InputStream stream = connection.getInputStream();
mString = CharStreams.toString(new InputStreamReader(stream, "UTF-8"));
}
return mString;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if (mWlqq!=null){
mWlqq.sueess(mString);
}
}
}
}
…
public void setQingq(String s, final String pwd, final String req){
new HttpUrl().getHttpUrl(s).setWlqq(new HttpUrl.Wlqq() {
@Override
public void sueess(String s) {
LoginBean loginBean = new Gson().fromJson(s, LoginBean.class);
LoginBean.DataBean data = loginBean.getData();
String ss=data.getMobile()+"";
if (data!=null){
if (data.getName().equals(pwd) &&ss.equals(req)){
mQiri.seccess(data.getName());
}else {
mQiri.failure();
}
}
}
});
}
public interface Qiri{
void seccess(String ss);
void failure();
}
private Qiri mQiri;
public void setQiri(Qiri qiri) {
mQiri = qiri;
}
}
public class ImageConFig {
public static ImageLoaderConfiguration getImageLoaderConfiguration(Context context){
ImageLoaderConfiguration build = new ImageLoaderConfiguration.Builder(context)
.memoryCacheSizePercentage(13)
.diskCacheSize(15*1024*1024)
.build();
return build;
}
public static DisplayImageOptions getDisplayImageOptions(Context context){
DisplayImageOptions build = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisk(true)
.showImageForEmptyUri(R.mipmap.ic_launcher)
.showImageOnFail(R.mipmap.ic_launcher)
.imageScaleType(ImageScaleType.EXACTLY)
.build();
return build;
}
}
public class MAdapter extends BaseAdapter {
private List<LeiBean.DataBean> mList;
private LayoutInflater mInflater;
public MAdapter(List<LeiBean.DataBean> list, LayoutInflater inflater) {
mList = list;
mInflater = inflater;
}
@Override
public int getCount() {
return mList.size();
}
@Override
public LeiBean.DataBean getItem(int i) {
return mList.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder vh;
if (view==null){
view=mInflater.inflate(R.layout.item,viewGroup,false);
vh=new ViewHolder(view);
}else {
vh= (ViewHolder) view.getTag();
}
vh.setBind(getItem(i));
return view;
}
class ViewHolder{
private ImageView mImage01,mImage02,mImage03;
private TextView t1,t2;
public ViewHolder(View view) {
mImage01 = view.findViewById(R.id.image01);
mImage02 = view.findViewById(R.id.image02);
mImage03 = view.findViewById(R.id.image03);
t1 = view.findViewById(R.id.it_t1);
t2 = view.findViewById(R.id.it_t2);
view.setTag(this);
}
public void setBind(LeiBean.DataBean lei){
ImageLoader.getInstance().displayImage(lei.getThumbnail_pic_s(), mImage01,ImageConFig.getDisplayImageOptions(mImage01.getContext()));
ImageLoader.getInstance().displayImage(lei.getThumbnail_pic_s02(), mImage02,ImageConFig.getDisplayImageOptions(mImage01.getContext()));
ImageLoader.getInstance().displayImage(lei.getThumbnail_pic_s03(), mImage03,ImageConFig.getDisplayImageOptions(mImage01.getContext()));
t1.setText(lei.getTitle());
t2.setText(lei.getAuthor_name());
}
}