linearlayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageView_linear_left"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/fanhui" />
<EditText
android:id="@+id/edit_linear"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1" />
<ImageView
android:id="@+id/imageView_linear_right"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="@drawable/tianjia" />
<ImageView
android:id="@+id/imageView_linear_chaxun"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="@drawable/pink_one" />
</LinearLayout>
CliearLayout
package com.bawei.lmx.day13_2.cliear;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.bawei.lmx.day13_2.R;
public class CliearLayout extends LinearLayout {
private ImageView imageView_linear_left;
private EditText edit_linear;
Call call;
private ImageView imageView_linear_right;
public CliearLayout(Context context) {
this(context, null);
}
public CliearLayout(Context context, AttributeSet attrs) {
this(context, attrs, 2);
}
public CliearLayout(final Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
View inflate = inflate(context, R.layout.linearlayout, this);
imageView_linear_left = inflate.findViewById(R.id.imageView_linear_left);
imageView_linear_right = inflate.findViewById(R.id.imageView_linear_right);
edit_linear = inflate.findViewById(R.id.edit_linear);
imageView_linear_right.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (call != null) {
String trim = edit_linear.getText().toString().trim();
call.result(trim);
} else {
Toast.makeText(context, "输入内容不能为空", Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
measureChildren(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int width = r - l;
int x = 0;
int y = 0;
int row = 1;
int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
View childAt = getChildAt(i);
x += childAt.getMeasuredWidth();
if (x > width) {
x = childAt.getMeasuredWidth();
row++;
}
y = row * childAt.getMeasuredHeight();
childAt.layout(x - childAt.getMeasuredWidth(), y - childAt.getMeasuredHeight(), x, y);
}
}
public void setCall(Call call) {
this.call = call;
}
public interface Call {
void result(String message);
}
}
activity_chaxun
<?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=".ChaxunActivity">
<com.bawei.lmx.day13_2.cliear.CliearLayout
android:id="@+id/cliearx"
android:layout_width="match_parent"
android:layout_height="200dp"></com.bawei.lmx.day13_2.cliear.CliearLayout>
</LinearLayout>
ChaxunActivity
package com.bawei.lmx.day13_2;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.bawei.lmx.day13_2.cliear.CliearLayout;
public class ChaxunActivity extends AppCompatActivity {
private CliearLayout cliearx;
private ImageView fanhui;
private static final String TAG = "ChaxunActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chaxun);
fanhui = findViewById(R.id.imageView_linear_left);
fanhui.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
cliearx = findViewById(R.id.cliearx);
cliearx.setCall(new CliearLayout.Call() {
@Override
public void result(final String message) {
if (!TextUtils.isEmpty(message)) {
TextView textView = new TextView(ChaxunActivity.this);
textView.setText(message);
textView.setTextSize(22);
textView.setTextColor(R.drawable.pink_one);
cliearx.addView(textView);
Log.i(TAG, "result: " + message);
textView.setPadding(20, 20, 20, 20);
Toast.makeText(ChaxunActivity.this, message, Toast.LENGTH_SHORT).show();
cliearx.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = getIntent();
intent.putExtra("value", message);
setResult(2, intent);
finish();
}
});
} else {
Toast.makeText(ChaxunActivity.this, "输入内容不能为空", Toast.LENGTH_SHORT).show();
}
}
});
}
}
返回到查询页面
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 || resultCode == 2) {
if (data != null) {
String value = data.getStringExtra("value");
Toast.makeText(getContext(), value, Toast.LENGTH_SHORT).show();
try {
encode = URLEncoder.encode(value, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
presenter.mutual(Url + "1");
}
}
}
MyApp
package com.bawei.lmx.day13_2;
import android.app.Application;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
public class MyApp0 extends Application {
private static RequestQueue requestQueue;
@Override
public void onCreate() {
super.onCreate();
requestQueue = Volley.newRequestQueue(getApplicationContext());
}
public static RequestQueue queue() {
return requestQueue;
}
}
HttpVolley
package com.bawei.lmx.day13_2;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import java.util.Map;
public class HttpVolley {
private static final HttpVolley ourInstance = new HttpVolley();
private StringRequest getRequest;
private StringRequest postRequest;
public static HttpVolley getInstance() {
return ourInstance;
}
private HttpVolley() {
}
public boolean isNet(Context context) {
if (context != null) {
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
if (networkInfo != null) {
return networkInfo.isAvailable();
}
}
return false;
}
public void VolleyHttpGet(String url, final VolleyCallBack callBack) {
getRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
callBack.onSuccess(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
callBack.onFail(error);
}
});
getRequest.setTag("gettest");
MyApp0.queue().add(getRequest);
}
public void VolleyHttpPost(String url, final Map<String, String> map, final VolleyCallBack callBack) {
postRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
callBack.onSuccess(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
callBack.onFail(error);
}
}) {
protected Map<String, String> getParams() throws AbstractMethodError, AuthFailureError {
return map;
}
};
postRequest.setTag("posttest");
MyApp0.queue().add(postRequest);
}
public interface VolleyCallBack {
void onSuccess(String data);
void onFail(VolleyError error);
}
}
MVP
Contract
package com.bawei.lmx.day13_2.mvp;
public interface Contract {
public interface ILoginView {
void getLoginResult(String result);
}
//view层接口
public interface IShowView {
//数据列表展示接口
void getResultData(String resultdata);
//数据详情接口
void getResultDedailsData(String resultdedails);
}
//model层接口
public interface IShowModel {
//数据请求方法
void ModelResult(String url, ModelCallBack modelCallBack);
interface ModelCallBack {
void Success(String result);
void Error();
}
//登录方法
void LoginModelResult(String url, String name, String pwd, LoginModelCallBack loginModelCallBack);
interface LoginModelCallBack {
void Success(String result);
void Error();
}
}
//presenter层接口
public interface IShowPresenter {
void attach(IShowView iShowView);
void detach();
void mutual(String url);
void mutualdedails(String url_dedails);
void login(String url, String name, String pwd);
void logindetach();
void loginattach(ILoginView iLoginView);
}
}
Model
package com.bawei.lmx.day13_2.mvp;
import android.util.Log;
import com.android.volley.VolleyError;
import com.bawei.lmx.day13_2.HttpVolley;
import java.util.HashMap;
import java.util.Map;
public class Model implements Contract.IShowModel {
private static final String TAG = "Model";
@Override
public void ModelResult(String url, final ModelCallBack modelCallBack) {
Log.d(TAG, "ModelResult: " + url);
HttpVolley.getInstance().VolleyHttpGet(url, new HttpVolley.VolleyCallBack() {
@Override
public void onSuccess(String data) {
modelCallBack.Success(data);
}
@Override
public void onFail(VolleyError error) {
}
});
}
@Override
public void LoginModelResult(String url, String name, String pwd, final LoginModelCallBack loginModelCallBack) {
Map<String, String> map = new HashMap<>();
map.put("phone", name);
map.put("pwd", pwd);
HttpVolley.getInstance().VolleyHttpPost(url, map, new HttpVolley.VolleyCallBack() {
@Override
public void onSuccess(String data) {
loginModelCallBack.Success(data);
}
@Override
public void onFail(VolleyError error) {
}
});
}
}
Presenter
package com.bawei.lmx.day13_2.mvp;
import android.util.Log;
public class Presenter implements Contract.IShowPresenter {
private Model loginmodel;
private Contract.ILoginView iloginview;
private Contract.IShowView ishowview;
private static final String TAG = "Presenter";
@Override
public void attach(Contract.IShowView iShowView) {
this.ishowview = iShowView;
loginmodel = new Model();
Log.d(TAG, "attach: " + ishowview.toString());
}
@Override
public void detach() {
}
@Override
public void mutual(String url) {
loginmodel.ModelResult(url, new Contract.IShowModel.ModelCallBack() {
@Override
public void Success(String result) {
ishowview.getResultData(result);
}
@Override
public void Error() {
}
});
}
@Override
public void mutualdedails(String url_dedails) {
}
@Override
public void login(String url, String name, String pwd) {
loginmodel.LoginModelResult(url, name, pwd, new Contract.IShowModel.LoginModelCallBack() {
@Override
public void Success(String result) {
iloginview.getLoginResult(result);
}
@Override
public void Error() {
}
});
}
@Override
public void logindetach() {
}
@Override
public void loginattach(Contract.ILoginView iLoginView) {
loginmodel = new Model();
this.iloginview = iLoginView;
}
}