# 接口回調(diào)進(jìn)行傳值——CallBack應(yīng)用舉例#
### 將數(shù)據(jù)以接口的形式外露
public class interface_class {
int aaa=3652;
int getdata(CALL call){
call.get_in(aaa);
return 1;
}
}
interface CALL {
void get_in(int abc);
}
看代碼:
public class callbacttest {
public static void main(String[] args) {
int x=? new interface_class().getdata(new CALL() {
@Override
public void get_in(int bbbb) {
System.out.println(bbbb);
}
});
System.out.println(x);
}
}
結(jié)果:
3652
1
---
#Toast優(yōu)化#
###(解決因Toast的隊(duì)列顯示問題)
import android.content.Context;
import android.widget.Toast;
/**
* Created by Administrator on 2016/9/11.
*/
public class Utils {
private static Toast mToast;
public static void showToast(Context context, String msg, int duration) {
if (mToast == null) {
mToast = Toast.makeText(context, msg, duration);
} else {
mToast.setText(msg);
}
mToast.show();
}
}
---
#popupWindow外部的事件就可以傳遞給下面的Activity#
那么,如果我想要一個(gè)效果,點(diǎn)擊外部區(qū)域,彈窗不消失,但是點(diǎn)擊事件會(huì)向下面的activity傳遞,比如下面是一個(gè)WebView,我想點(diǎn)擊里面的鏈接等.
研究了半天谍失,說是要給Window設(shè)置一個(gè)Flag,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
看了源碼虑灰,這個(gè)Flag的設(shè)置與否是由一個(gè)叫mNotTouchModal的字段控制微王,但是設(shè)置該字段的set方法被標(biāo)記為@hide旋圆。
所以要通過反射的方法調(diào)用:
復(fù)制代碼
/**
* Set whether this window is touch modal or if outside touches will be sent
* to
* other windows behind it.
*
*/
public static void setPopupWindowTouchModal(PopupWindow popupWindow,
boolean touchModal) {
if (null == popupWindow) {
return;
}
Method method;
try {
method = PopupWindow.class.getDeclaredMethod("setTouchModal",
boolean.class);
method.setAccessible(true);
method.invoke(popupWindow, touchModal);
}
catch (Exception e) {
e.printStackTrace();
}
}
復(fù)制代碼,然后在程序中:
UIUtils.setPopupWindowTouchModal(popupWindow, false);
該popupWindow外部的事件就可以傳遞給下面的Activity了尸闸。
#切換后將EditText光標(biāo)置于末尾#
charSequence = etdatelimit.getText();
if (charSequence instanceof Spannable) {
Spannable spanText = (Spannable) charSequence;
Selection.setSelection(spanText, charSequence.length());
}
#文件上傳#
```
/**
* @param path 要上傳的文件路徑
* @throws Exception
*/
public void uploadFile(String path) throws Exception {
String url = "http://116.255.193.42:8080/Handler.ashx";
File file = new File(path);
if (file.exists() && file.length() > 0) {
AsyncHttpClient client = new AsyncHttpClient();
com.loopj.android.http.RequestParams requestParams = new com.loopj.android.http.RequestParams();
requestParams.put("uploadfile", file);
// 上傳文件
client.post(url, requestParams, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers,
byte[] responseBody) {
// 上傳成功后要做的工作
Toast.makeText(ImageDetailActivity.this, "上傳成功", Toast.LENGTH_LONG).show();
//? ? ? ? ? ? ? ? ? ? progress.setProgress(0);
}
@Override
public void onFailure(int statusCode, Header[] headers,
byte[] responseBody, Throwable error) {
// 上傳失敗后要做到工作
Toast.makeText(ImageDetailActivity.this, "上傳失敗", Toast.LENGTH_LONG).show();
}
@Override
public void onRetry(int retryNo) {
// TODO Auto-generated method stub
super.onRetry(retryNo);
// 返回重試次數(shù)
}
@Override
public void onProgress(long bytesWritten, long totalSize) {
super.onProgress(bytesWritten, totalSize);
int count = (int) ((bytesWritten * 1.0 / totalSize) * 100);
// 上傳進(jìn)度顯示
Log.e("上傳 Progress>>>>>", bytesWritten + " / " + totalSize);
}
});
} else {
Toast.makeText(ImageDetailActivity.this, "文件不存在", Toast.LENGTH_LONG).show();
}
}
```
```
/**
* 屏幕旋轉(zhuǎn)時(shí)調(diào)用此方法
*/
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
//? ? ? ? ? ? Toast.makeText(MainActivity.this, "現(xiàn)在是豎屏", Toast.LENGTH_SHORT).show();
fl_bottom_menu.setVisibility(View.VISIBLE);
}
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
//? ? ? ? ? ? Toast.makeText(MainActivity.this, "現(xiàn)在是橫屏", Toast.LENGTH_SHORT).show();
fl_bottom_menu.setVisibility(View.GONE);
}
```
}
#? 點(diǎn)擊監(jiān)聽接口
```
public class HBDialogRead {
private Dialog dialog;
private Context context;
private OnOKClickListener okClickListener;
public HBDialogRead(Context context) {
this.context = context;
}
public void showDialog(String coin) {
dialog = new Dialog(context, R.style.my_hb_dialog);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉自定義對話框的tiltle
dialog.setContentView(R.layout.hb_dialog_read);//設(shè)置自定義Dialog視圖布局
Window window = dialog.getWindow();
window.setGravity(Gravity.CENTER);
dialog.show();
TextView tv_coin = (TextView) dialog.findViewById(R.id.tv_coin);
Button bt_ok = (Button) dialog.findViewById(R.id.bt_ok);
ImageView iv_close = (ImageView) dialog.findViewById(R.id.iv_close);
tv_coin.setText(coin + "");
bt_ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (okClickListener != null) {
okClickListener.onOKClick();
}
dialog.dismiss();
}
});
iv_close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
}
public void setOnOKClickListener(OnOKClickListener okClickListener) {
this.okClickListener = okClickListener;
}
public interface OnOKClickListener {
void onOKClick();
}
}
```