27
2017
09

Android随笔记

接口回调进行传值——CallBack应用举例

将数据以接口的形式外露

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);
        }
}

结果:

3652

1


Toast优化

(解决因Toast的队列显示问题)

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

那么,如果我想要一个效果,点击外部区域,弹窗不消失,但是点击事件会向下面的activity传递,比如下面是一个WebView,我想点击里面的链接等.

  研究了半天,说是要给Window设置一个Flag,
  WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
  
  看了源码,这个Flag的设置与否是由一个叫mNotTouchModal的字段控制,但是设置该字段的set方法被标记为@hide。
  所以要通过反射的方法调用:
复制代码
/**
* 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();
}

}

复制代码,然后在程序中:  

UIUtils.setPopupWindowTouchModal(popupWindow, false);

  该popupWindow外部的事件就可以传递给下面的Activity了。

切换后将EditText光标置于末尾

            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);
                    // 返回重试次数
                }

                @Override
                public void onProgress(long bytesWritten, long totalSize) {
                    super.onProgress(bytesWritten, totalSize);
                    int count = (int) ((bytesWritten * 1.0 / totalSize) * 100);
                    // 上传进度显示
                    Log.e("上传 Progress>>>>>", bytesWritten + " / " + totalSize);
                }
            });
        } else {
            Toast.makeText(ImageDetailActivity.this, "文件不存在", Toast.LENGTH_LONG).show();
        }
    }
/** * 屏幕旋转时调用此方法 */
 @Override
 public void onConfigurationChanged(Configuration newConfig) {
 super.onConfigurationChanged(newConfig);
 if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
// Toast.makeText(MainActivity.this, "现在是竖屏", Toast.LENGTH_SHORT).show();
 fl_bottom_menu.setVisibility(View.VISIBLE);
 }
 if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
// Toast.makeText(MainActivity.this, "现在是横屏", Toast.LENGTH_SHORT).show();
 fl_bottom_menu.setVisibility(View.GONE);
 }

}

点击监听接口

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);//设置自定义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();
    }
}
上一篇:第一篇:基本框架(基类,网络框架,主页面) 下一篇:ViewPager+Fragment(小圆点)