代码小知识
来源:互联网 发布:淘宝美工职责 编辑:程序博客网 时间:2024/05/16 21:21
一:得到屏幕的宽度(两种方法):
1:
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
dm.widthPixels获取屏幕宽
dm.heightPixels获取屏幕高度
2:
screenWidth = context.getWindowManager().getDefaultDisplay().getWidth()
screenHeightt = context.getWindowManager().getDefaultDisplay().getHeight()
public String refFormatNowDate() {
Date nowTime = new Date(System.currentTimeMillis());
SimpleDateFormat sdFormatter = new SimpleDateFormat("yyyy-MM-dd");
String retStrFormatNowDate = sdFormatter.format(nowTime);
return retStrFormatNowDate;
}
三,动态设置布局:
LinearLayout.LayoutParams vl = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
// vl.setMargins(Constants.dip2px(mContext,Constants.px2dip(mContext,20)), 0, Constants.dip2px(mContext,Constants.px2dip(mContext,50)), 0);
vl.setMargins(Constants.dip2px(mContext,20),0,Constants.dip2px(mContext,30),0);
vl.gravity = Gravity.CENTER_VERTICAL;
textView.setLayoutParams(vl);
textView.setGravity(Gravity.CENTER);
// textView.setPadding(Constants.dip2px(mContext,Constants.px2dip(mContext,38)),0,Constants.dip2px(mContext,Constants.px2dip(mContext,38)),0);
四:键盘的控制
// 键盘消失
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
// 键盘出现
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.RESULT_SHOWN);
// 在点击事件中加入,如果键盘存在,让软键盘消失
try {
((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(GroupChatActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}catch (Exception e){
e.printStackTrace();
}
((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(GroupChatActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}catch (Exception e){
e.printStackTrace();
}
在清单文件中添加,是页面在一开始不显示软键盘:(有问题)
android:windowSoftInputMode="stateHidden"
默认不显示软键盘:
在oncreate方法里面加-------》
getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
五:验证时间倒计时
mBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mBtn.setEnabled(false);
mBtn.post(new Runnable() {
@Override
public void run() {
mBtn.setText("剩余时间"+time+"秒");
if(time>1){
time--;
mBtn.postDelayed(this,1000);
}else{
time = 20;
}
}
});
}
});
六:控制一个控件的消失和显示
boolean visible = mMoreLayout.getVisibility() == View.VISIBLE;
view.setVisibility(visible ? View.GONE : View.VISIBLE);
七: 弹框
/**
* 弹出右上角号码群操作窗口
*/
private void showGroupOpreatePop(){
final View mainView = mInflater.inflate(R.layout.cloud_group_operation_pop_layout,null);
LinearLayout btnCreateCircle = (LinearLayout) mainView.findViewById(R.id.ll_create_circle);
LinearLayout btnCreateYellowPage = (LinearLayout) mainView.findViewById(R.id.ll_create_yellowpage);
TextView btnSubscribeGroup = (TextView) mainView.findViewById(R.id.tv_subscribe_group);
btnCreateCircle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mOperatePopWindow.dismiss();
}
});
btnCreateYellowPage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mOperatePopWindow.dismiss();
}
});
btnSubscribeGroup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mOperatePopWindow.dismiss();
Intent intent = new Intent(mActivity, SubscribeListActivity.class);
startActivity(intent);
}
});
int popWidth = Constants.dip2px(mActivity,180);
int popHeight = Constants.dip2px(mActivity,168);
mOperatePopWindow = new PopupWindow(mainView,
popWidth,
popHeight);
mOperatePopWindow.setFocusable(true);
mOperatePopWindow.setOutsideTouchable(true);
int xOffSet = mActivity.getWindowManager().getDefaultDisplay().getWidth() - popWidth - 16;
int yOffSet = Constants.dip2px(mActivity,14);
mOperatePopWindow.setBackgroundDrawable(new ColorDrawable(
Color.TRANSPARENT));
mOperatePopWindow.showAsDropDown(mTitleBar,xOffSet,-yOffSet);
AlphaAnimation aa = new AlphaAnimation(0.4f, 1.0f);
aa.setDuration(250);
ScaleAnimation sa = new ScaleAnimation(0.2f, 1.0f, 0.2f,
1.0f, Animation.RELATIVE_TO_SELF, 0.91f,
Animation.RELATIVE_TO_SELF, 0f);
sa.setDuration(250);
AnimationSet set = new AnimationSet(false);
set.addAnimation(aa);
set.addAnimation(sa);
mainView.startAnimation(set);
}
0 0
- 代码小知识
- 代码小知识2
- 代码小知识3
- 黑马程序员--代码块小知识总结
- 『小知识』网页中禁止复制的代码
- OllyDbg小知识【1】修改代码后保存
- Android 原生代码开发的一些小知识
- 有关shell的几个小知识,有代码
- 《第一行代码》学后知识小总结
- 小知识
- 小知识
- 小知识
- 小知识
- 小知识
- 小知识
- 小知识
- 小知识
- 小知识
- 百度Android定位SDK实现获取当前经纬度及位置
- 2014学习总结
- Unity之Shader在模型上渲染积雪效果-三
- jQuery.Autocomplete实现自动完成功能(详解)
- [leetcode 15] 3Sum
- 代码小知识
- JAVA程序设计(20)----- 查询信息的数据库代码
- GDB寄存器和内存
- 2014年过去与未来
- SQL Server2005+SQL Server2000下载
- include重复id的监听
- MySQL Proxy简介
- 我的第一篇博客----2014-12-31
- 一声叹息又一年