布局 login_activity
<?xml version="1.0" encoding="utf-8"?><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:text="简易登录"android:textSize="30sp" /><Viewandroid:layout_width="match_parent"android:layout_height="2dp"android:layout_marginTop="10dp"android:layout_marginBottom="10dp"android:background="#ccc" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="用户名:"android:textSize="20sp" /><EditTextandroid:id="@+id/ed_name"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:hint="请输入用户名..." />
</LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="密码:"android:textSize="20sp" /><EditTextandroid:id="@+id/ed_pwd"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:hint="请输入密码..."android:password="true" />
</LinearLayout><Viewandroid:layout_width="match_parent"android:layout_height="2dp"android:layout_marginTop="10dp"android:layout_marginBottom="10dp"android:background="#ccc" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><CheckBoxandroid:id="@+id/btn_jzpwd"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:text="记住密码" /><CheckBoxandroid:id="@+id/btn_bcpwd"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:text="保存密码" />
</LinearLayout><Buttonandroid:id="@+id/btn_login"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="100dp"android:text="登录登录" />
<Buttonandroid:id="@+id/login_reg"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="注册" />
**注册布局** <?xml version="1.0" encoding="utf-8"?>
<TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:text="简易注册"android:textSize="30sp" /><Viewandroid:layout_width="match_parent"android:layout_height="2dp"android:layout_marginTop="10dp"android:layout_marginBottom="10dp"android:background="#ccc" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="用户名:"android:textSize="20sp" /><EditTextandroid:id="@+id/ed_zcname"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:hint="请输入用户名..." />
</LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="密码:"android:textSize="20sp" /><EditTextandroid:id="@+id/ed_zcpwd"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:hint="请输入密码..." />
</LinearLayout><Viewandroid:layout_width="match_parent"android:layout_height="2dp"android:layout_marginTop="10dp"android:layout_marginBottom="10dp"android:background="#ccc" /><Buttonandroid:id="@+id/btn_register"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="100dp"android:text="注册注册" />
**刷新布局** <?xml version="1.0" encoding="utf-8"?>
<com.handmark.pulltorefresh.library.PullToRefreshListViewandroid:id="@+id/pullto"android:layout_width="match_parent"android:layout_height="match_parent" />
**生成二维码** <?xml version="1.0" encoding="utf-8"?>
<TextViewandroid:id="@+id/sou"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:ems="10"android:text="扫一扫有惊喜~" /><ImageViewandroid:id="@+id/img"android:layout_width="300dp"android:layout_height="300dp"android:layout_gravity="center_horizontal"android:layout_marginTop="20dp" />
LoginActivity主页面
ample.lg.lx_1207;
t.Context;
t.Intent;
t.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import bean.News;
import bean.User;
import core.DataCall;
import presenter.LoginPresenter;
public class LoginActivity extends AppCompatActivity implements View.OnClickListener, DataCall {
/*** 请输入用户名...*/
private EditText mEdName;
/*** 请输入密码...*/
private EditText mEdPwd;
/*** 记住密码*/
private CheckBox mBtnJzpwd;
/*** 保存密码*/
private CheckBox mBtnBcpwd;
/*** 登录登录*/
private Button mBtnLogin;
LoginPresenter presenter = new LoginPresenter(this);
/*** 注册*/
private Button mLoginReg;
private String mobile;
private String password;
private SharedPreferences sp;@Override
protected void onCreate(Bundle savedInstanceState) {Create(savedInstanceState);setContentView(R.layout.activity_login);//初始化控件initView();//创建spsp = getSharedPreferences(", Context.MODE_PRIVATE);//判断是否记住密码rememberPwd();
}//记住密码
private void rememberPwd() {//获取传来的账号密码String getmoilde = sp.getString("putmoilde", "");String getpwd = sp.getString("putpwd", "");boolean getremember = sp.getBoolean("putremember", false);//判断选中状态if (getremember) {//选中设置 默认登录显示mEdName.setText(getmoilde);mEdPwd.setText(getpwd);//设置选中mBtnJzpwd.setChecked(getremember);} else {//未选中为空mEdName.setText("");mEdPwd.setText("");mBtnJzpwd.setChecked(false);}
}//初始化控件
private void initView() {mEdName = findViewById(R.id.ed_name);mEdPwd = findViewById(R.id.ed_pwd);mBtnJzpwd = findViewById(R.id.btn_jzpwd);mBtnBcpwd = findViewById(R.id.btn_bcpwd);mBtnLogin = findViewById(R.id.btn_login);mBtnLogin.setOnClickListener(this);mLoginReg = findViewById(R.id.login_reg);mLoginReg.setOnClickListener(this);
}@Override
public void onClick(View v) {switch (v.getId()) {default:break;case R.id.btn_login://登录获取输入的账户密码mobile = Text().toString();password = Text().toString();//p层方法quest(mobile, password);break;case R.id.login_reg://注册页面跳转Intent intent = new Intent(this, RegisterActivity.class);startActivity(intent);break;}
}//调用接口
@Override
public void callback(Object data) {//数据转型User user = (User) data;Toast.makeText(getBaseContext(), Code() + " " + Msg(), Toast.LENGTH_LONG).show();//*********获取记住密码勾选状态boolean remember = mBtnJzpwd.isChecked();SharedPreferences.Editor edit = sp.edit();//判断是否记住密码if (remember) {//勾选传值edit.putString("putmoilde", mobile);edit.putString("putpwd", password);edit.putBoolean("putremember", true);} else {//未勾选 复选框未选中edit.putBoolean("putremember", false);}editmit();//登录跳转Intent intent = new Intent(LoginActivity.this, ShowActivity.class);intent.putExtra("name", mobile);//传入用户名 做二维码startActivity(intent);
}@Override
public void faild(String error) {//异常吐司Toast.makeText(getBaseContext(), " 有问题啊老铁", Toast.LENGTH_LONG).show();
}@Override
public void showcallback(News news) {}
}
RegisterActivity注册
ample.lg.lx_1207;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import bean.News;
import bean.User;
import core.DataCall;
import presenter.RegisterPresenter;
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener, DataCall {
/*** 请输入用户名...*/
private EditText mEdZcname;
/*** 请输入密码...*/
private EditText mEdZcpwd;
/*** 注册注册*/
private Button mBtnRegister;
RegisterPresenter presenter = new RegisterPresenter(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
Create(savedInstanceState);
setContentView(R.layout.activity_register);
initView();
}
private void initView() {mEdZcname = findViewById(R.id.ed_zcname);mEdZcpwd = findViewById(R.id.ed_zcpwd);mBtnRegister = findViewById(R.id.btn_register);mBtnRegister.setOnClickListener(this);
}@Override
public void onClick(View v) {switch (v.getId()) {default:break;case R.id.btn_register:final String mobile = Text().toString();final String password = Text().toString();quest(mobile, password);break;}
}@Override
public void callback(Object data) {User user = (User) data;Toast.makeText(getBaseContext(),Code()+" "Msg(),Toast.LENGTH_LONG).show();finish();
}@Override
public void faild(String error) {Toast.makeText(getBaseContext(),"有问题啊老铁"+error,Toast.LENGTH_LONG).show();
}@Override
public void showcallback(News news) {}
}
BasePrsenter
package presenter;
import android.os.Handler;
import android.os.Message;
import bean.User;
import core.DataCall;
/**
P层基类----注意抽象类
*/
public abstract class BasePresenter {
private final DataCall call;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
//******super删掉
//取得消息–转成封装bean类
User user = (User) msg.obj;
//获取状态码
String u = Code();
//强转int—做判断
int u2 = Integer.parseInt(u);
//为0则成功 调用 正确接口
if (u2 == 0) {
call.callback(msg.obj);
} else {//不为0 调用 错误接口call.faild(u);}}
};
//重写P层调用接口
public BasePresenter(DataCall dataCall) {
call = dataCall;
}
/**
public void request(final String… args) {
new Thread() {
@Override
public void run() {
super.run();
//获取数据
Object data = useModel(args);
//handler消息
Message message = handler.obtainMessage();
message.obj = data;
//发送消息
handler.sendMessage(message);
}
}.start();
}
}
LoginPresenter
package presenter;
import core.DataCall;
import model.LoginModel;
/***
登录P层----继承基类
*/
public class LoginPresenter extends BasePresenter {
public LoginPresenter(DataCall dataCall) {
super(dataCall);
}
@Override
public Object useModel(String… args) {
//返回 登录M
return LoginModel.login(args[0], args[1]);
}
}
RegisterPresenter
package presenter;
import core.DataCall;
import model.RegisterModel;
/***
注册P层----继承基类
*/
public class RegisterPresenter extends BasePresenter {
public RegisterPresenter(DataCall dataCall) {
super(dataCall);
}
@Override
public Object useModel(String… args) {
//返回注册M层
ister(args[0], args[1]);
}
}
ShowPresenter
package presenter;
import android.os.Handler;
import android.os.Message;
import bean.News;
import core.DataCall;
import model.ShowModel;
/**
展示P层 接口不同 需要自己写
*/
public class ShowPresenter {
private final DataCall dataCall;
//重写 调用接口
public ShowPresenter(DataCall back) {
dataCall = back;
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
News news = (News) msg.obj;
//调用接口
dataCall.showcallback(news);
}
};
//sel方法–子线程
public void sel(final int page) {
new Thread() {
@Override
public void run() {
super.run();
//获取数据
News news = ShowModel.sel(page);
Message message = handler.obtainMessage();
message.obj = news;
//发送handler
handler.sendMessage(message);
}
}.start();
}
}
httpUtils
package utils;
import android.util.Log;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.HttpURLConnection;
import java.MalformedURLException;
import java.URL;
public class HttpUtils {
private static final String TAG = “HttpUtils+++++”;
public static String get(String urlString) {
try {
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream inputStream = InputStream();//获取网络返回的输入流;
//可拼接的字符串
StringBuilder stringBuilder = new StringBuilder();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String temp = “”;
while ((temp = adLine()) != null) {
stringBuilder.append(temp);
temp = “”;
}
//这个是网络获取的数据
String data = String();
Log.d(TAG, “get: +++++”+data);
return data;
} catch (Exception e) {
e.printStackTrace();
}
return “”;
}
}
modelLogin
package model;
le.gson.Gson;
import bean.User;
import utils.HttpUtils;
/**
le.gson.Gson;
import bean.News;
import utils.HttpUtils;
/**
import bean.News;
import bean.User;
/**
接口
*/
public interface DataCall {
//正确接口
void callback(Object data);
//错误接口
void faild(String error);
//展示接口
void showcallback(News news);
}
MyApp
package app;
import android.app.Application;
t.Context;
aphics.Bitmap;
import android.os.Environment;
import android.os.Handler;
stra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
stra13.universalimageloader.cache.disc.naming.HashCodeFileNameGenerator;
stra13.impl.LruMemoryCache;
DisplayImageOptions;
ImageLoader;
ImageLoaderConfiguration;
assist.QueueProcessingType;
decode.BaseImageDecoder;
display.SimpleBitmapDisplayer;
download.BaseImageDownloader;
stra13.universalimageloader.utils.StorageUtils;
import java.io.File;
/**
public class MyApp extends Application {
private static Context context;
@Override
public void onCreate() {
Create();
context=this;
// 初始化imageloa
Instance().init(getConfig());
}
// 全局的上下文
public static Context getContext(){
return context;
}
public ImageLoaderConfiguration getConfig() {String path = ExternalStorageDirectory() + "/imagefile";File cacheDir = new File(path);if (!ists()) {cacheDir.mkdir();}ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this).diskCache(new UnlimitedDiskCache(cacheDir)) // default 闪存缓存.diskCacheSize(50 * 1024 * 1024) // 闪存缓存大小.writeDebugLogs() // LOG.build();return config;
}// 配置
public static DisplayImageOptions getOptions() {DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true) // default.cacheOnDisk(true) // default.bitmapConfig(Bitmap.Config.ARGB_4444) // default.displayer(new SimpleBitmapDisplayer()) // default.handler(new Handler()) // default.build();return options;
}
}
NewSAdapter优化
package adapters;
t.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
ample.lg.lx_1207.R;
ImageLoader;
import java.util.List;
import app.MyApp;
import bean.News;
/**
public class NewsAdapter extends BaseAdapter {
private Context context;
private List<News.NewslistBean> list;
public NewsAdapter(Context context, List<News.NewslistBean> list) {t = context;this.list = list;
}@Override
public int getCount() {return list.size();
}@Override
public Object getItem(int position) {(position);
}@Override
public long getItemId(int position) {return position;
}@Override
public View getView(int position, View convertView, ViewGroup parent) {ViewHodel hodel;if (convertView == null) {convertView = View.inflate(context, R.layout.itemlayout, null);hodel = new ViewHodel();_title = convertView.findViewById(view);hodel.img_images = convertView.findViewById(R.id.imageview);convertView.setTag(hodel);} else {hodel = (ViewHodel) Tag();}_title.(position).getTitle());Instance().(position).getPicUrl(), hodel.img_images, Options());return convertView;
}class ViewHodel {TextView text_title;ImageView img_images;
}
}
Fragment01
package fragment;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
ample.lg.lx_1207.R;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import java.util.List;
import adapters.NewsAdapter;
import app.MyApp;
import bean.News;
import core.DataCall;
import presenter.ShowPresenter;
public class Fragment1 extends Fragment implements DataCall {
private PullToRefreshListView listView;
private int type = 0;
private int page = 1;
private List<News.NewslistBean> list;
private NewsAdapter adapter;
private ShowPresenter presenter;@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {View view = inflater.inflate(R.layout.fragment1, container, false);//初始化控件listView = view.findViewById(R.id.pullto);presenter = new ShowPresenter(this);presenter.sel(page);listView.setMode(PullToRefreshBase.Mode.BOTH);listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {@Overridepublic void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {type = 1;page = 1;getDataForNet(type, page);}@Overridepublic void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {type = 2;page++;getDataForNet(type, page);}});return view;
}private void getDataForNet(int type, int page) {switch (type) {case ifyDataSetChanged();break;case ifyDataSetChanged();RefreshComplete();break;case 2:presenter.sel(page);ifyDataSetChanged();RefreshComplete();break;}
}@Override
public void callback(Object data) {}@Override
public void faild(String error) {}@Override
public void showcallback(News news) {list = Newslist();//建立适配器adapter = new Context(), list);listView.setAdapter(adapter);if (type == 2) {List<News.NewslistBean> list1 = Newslist();list.addAll(list1);type = 0;}
}
}
Fragment02
package fragment;
t.Intent;
aphics.Bitmap;
aphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
ample.lg.lx_1207.R;
import cn.ing.QRCodeEncoder;
public class Fragment2 extends Fragment {
private static final String TAG = “Fragment2++++”;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Bitmap bitmap = (Bitmap) msg.obj;
imageView.setImageBitmap(bitmap);
}
};
private ImageView imageView;
private String name;@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {View view = inflater.inflate(R.layout.fragment2, container, false);imageView = view.findViewById(R.id.img);Intent intent = getActivity().getIntent();name = StringExtra("name");Log.d(TAG, "onCreateView: ++++" + name);new Thread(new Runnable() {@Overridepublic void run() {Bitmap bitmap = QRCodeEncoder.syncEncodeQRCode(name, 300);Message message = handler.obtainMessage();message.obj = bitmap;handler.sendMessage(message);}}) {}.start();return view;
}
}
本文发布于:2024-01-31 03:17:14,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170664223524967.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |