布局:覆盖子类控件直接获得焦点
android:descendantFocusability="blocksDescendants"
EditText
EditText:输入类型,输入字数限制,最大行数,省略号的显示位置
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:maxLength="50"
android:maxLines="2"
android:ellipsize="end"/>
设置输入类型为密码时发现可以输入中文,这当然不行!!
使用digits!更为精准的限制输入类型的方法!
android:inputType="textPassword"
android:maxLength="16"
android:digits="@string/input_pwd_format"
android:id="@+id/et_pwd"
android:hint="@string/password_login"
我设置为只能输入数字和字母,string里面的内容就是你的输入框允许出现的字符
<string name="input_pwd_format">0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ</string>
有时使用et输入文字发现光标在最前方,代码设置:
etData.setText(message);
etData.setSelection(message.length());// 使光标置于文末
取消et焦点,在其父控件设置
android:focusable="true"
android:focusableInTouchMode="true"
et第一个显示光标,第二个设置光标颜色,@null为输入的字体颜色
android:cursorVisible="true"
android:textCursorDrawable="@null"
et设置只能输入整数(小数为decimal)
android:numeric="integer"
et设置多行显示
android:inputType="textMultiLine" //可以显示多行
android:gravity="left|top" //光标在左上方
android:minLines="6" //最少显示6行
et代码设置字数限制
etData.setHint("最多输入100个字");
etData.setFilters(new InputFilter[]{new InputFilter.LengthFilter(100)});
et禁止输入
android:focusable="false"
设置显示电话号码中间四位为*
String tel="12345678901";
phone = tel.substring(0, 3) + "****" + tel.substring(7, tel.length());
addTextChangedListener方法添加输入内容的变化的事件监听
控制输入结果为>0的两位小数
private static final int DECIMAL_DIGITS = 2;//小数的位数
@OnTextChanged(value = R.id.et_distance, callback = OnTextChanged.Callback.TEXT_CHANGED)
void onDistanceTextChanged(Editable s) {
String str;
if (s.toString().contains(".")) {
if (s.length() - 1 - s.toString().indexOf(".") > DECIMAL_DIGITS) {
str = s.toString().substring(0, s.toString().indexOf(".") + DECIMAL_DIGITS + 1);
etDistance.setText(str);
etDistance.setSelection(str.length());
}
}
if (s.toString().trim().substring(0).equals(".")) {
str = "0" + s;
etDistance.setText(str);
etDistance.setSelection(2);
}else if (s.length() > 0) {
if (!(Double.valueOf(s.toString()) > 0)) {
etDistance.setText("3.00");
etDistance.setSelection(4);
}
}
if (s.toString().startsWith("0") && s.toString().trim().length() > 1) {
if (!s.toString().substring(1, 2).equals(".")) {
etDistance.setText(s.subSequence(0, 1));
etDistance.setSelection(1);
return;
}
}
}
et输入时改变背景,只要建一个背景selector选择器,分别在不同的状态下配置不同的背景e.g.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/blue_bg" android:state_focused="true" />
<item android:drawable="@drawable/gray_bg" android:state_focused="false" />
</selector>
但有一种情况是et聚焦时要改变的是它父容器的背景,这里有个属性android:addStatesFromChildren=”true”,设为true的话,父容器的状态跟随子控件的状态变化。
android:addStatesFromChildren="true"
TextView
给textview加横线
tvPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);// 中间加横线
tvPrice.getPaint().setFlags(Paint. UNDERLINE_TEXT_FLAG );// 下划线
设置文字样式
String text="约5分钟";
// 第一种方式Spannable改变字符串指定位置文字大小
Spannable span = Spannable.Factory.getInstance().newSpannable(text);
RelativeSizeSpan big = new RelativeSizeSpan(2);
span.setSpan(big, 1, text.length() - 2, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
holder.tvTime.setText(span);
// 第二种方式HTML改变文字样式
String frontText = text.substring(0, 1);
String afterText = datelist.get(position).getPhone().substring(1);
String colorText = "<font color=\"#3AA3EE\">" + frontText + "</font>" + afterText;
holder.tvPhone.setText(Html.fromHtml(colorText));
// 字体大小small,big
holder.tvPrice.setText(Html.fromHtml("<font><small>¥</small></font>"+String.format("%.2f",goodsList.get(i).getSales_price())));
// SpannableStringBuilder改变指定位置的文字颜色
public static CharSequence matcherSearchText(int color, String string, String keyWord) {
SpannableStringBuilder builder = new SpannableStringBuilder(string);
int indexOf = string.indexOf(keyWord);
if (indexOf != -1) {
builder.setSpan(new ForegroundColorSpan(color), indexOf, indexOf + keyWord.length(), SPAN_EXCLUSIVE_EXCLUSIVE);
}
return builder;
}
加载html
content.setText(Html.fromHtml((String)jsonObject.get("content")));
跑马灯效果,要取得焦点才会滚动起来
android:focusable="true"
android:focusableInTouchMode="true"
android:ellipsize="marquee"
android:singleLine="true"
取消listview item的点击效果
android:listSelector="@color/transparent"
代码字体加粗效果
textView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
Button
解决Android5.0以上版本自带阴影效果的方法
style="?android:attr/borderlessButtonStyle"
解决底部button随软键盘弹出上移,清单文件中该类添加输入法控制
android:windowSoftInputMode="stateHidden|adjustPan"
Json对象中是否存在某个key
jsonObject.has("String name");
自动弹出软键盘
// edittext获得焦点
etData.setFocusable(true);
etData.setFocusableInTouchMode(true);
etData.requestFocus();
// 界面跳转可能因为未加载完全而导致键盘不能弹出,此时设置延迟保证键盘的顺利弹出
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
// 调用输入管理器弹出软键盘
InputMethodManager inputManager =
(InputMethodManager) etData.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.showSoftInput(etData, 0);
}
},
998);
CheckBox
自定义样式,button属性要改成null
<CheckBox
android:id="@+id/check"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/checkbox_selector"
android:button="@null" />
选中时et输入,spinner不能点击,不选时et禁止输入,spinner可点击
cbOther.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
llType.setClickable(false);
llType.setBackgroundColor(Color.GRAY);
tvType.setText("选择车型");
etType.setFocusable(true);
etType.setFocusableInTouchMode(true);
etType.setEnabled(true);
}else {
llType.setClickable(true);
etType.setEnabled(false);
etType.setFocusable(false);
etType.setFocusableInTouchMode(false);
llType.setBackgroundColor(Color.WHITE);
}
}
});
ListView
Android实现ListView或GridView首行/尾行距离屏幕边缘距离
描述: ListView或GridView首行/尾行距离失效。
原因: Android上ListView&GridView默认行都是置顶的。
解决:设置ListView或GridView的android:clipToPadding = true,然后通过paddingTop和paddingBottom设置距离就好了
ExpandableListView
android:cacheColorHint="#00000000",这个设置可以去除拖动view时背景变成黑色的效果
android:listSelector="#00000000" ,可以去除选中时的黄色底色
去掉分割线最好设置成背景色而不是设成@null,设置@null运行时没问题,但可能会导致组名显示不完全,点击列表奔溃等问题。
android:cacheColorHint="#00000000"
android:childDivider="#fff"
android:divider="#fff"
android:listSelector="#00000000"
// 展开分组
for (int i = 0; i < gruops.size(); i++) {
elvConfig.expandGroup(i);
}
// 禁止点击收缩
elvConfig.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
return true;
}
});
获取返回数据类型的方法
先取得该数据所在的类,再获得类名
LogOut(jsonObj.get("skill_type").getClass().getName(),"==========skill_type===========");
LogOut(jsonObj.get("car_type_str").getClass().getName(),"==========car_type_str===========");
I/============skill_type=============: ==java.util.ArrayList==
I/============car_type_str=============: ==java.lang.String==
从assets 文件夹中获取文件并读取数据
getFromAssets("addr.txt")
public String getFromAssets(String fileName) {
String result = "";
try {
InputStream in = getResources().getAssets().open(fileName);
//获取文件的字节数
int lenght = in.available();
//创建byte数组
byte[] buffer = new byte[lenght];
//将文件中的数据读到byte数组中
in.read(buffer);
result = new String(buffer, "GBK");
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
侧滑栏焦点
网上大多数DrawerLayout的实例都是跟google一样,一个listview,所以listview会获得焦点,事件就不会传递了。但是用include加载的布局,会出现底部content获得事件的情况
解决办法:在include进的那个布局里面,添加clickable=true
AppComatActivity背景透明实现
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<!--全屏加透明-->
<style name="TranslucentFullScreenTheme" parent="FullScreenTheme">
<item name="android:windowBackground">@color/translate</item>
<item name="android:windowIsTranslucent">true</item>
</style>
<!--全屏-->
<style name="FullScreenTheme" parent="AppTheme">
<item name="android:windowFullscreen">true</item>
<item name="android:windowNoTitle">true</item>
</style>
<!--透明,有任务栏电量时间等-->
<style name="NoTitleTranslucentTheme" parent="AppTheme.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@color/translate</item>
<item name="android:windowIsTranslucent">true</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
WebView
WebView默认是不处理https请求的,页面显示空白,需要进行如下设置
在WebViewClient子类中重写父类的onReceivedSslError函数 代码如下:
webView.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed(); // 接受信任所有网站的证书
// handler.cancel(); // 默认操作 不处理
// handler.handleMessage(null); // 可做其他处理
}
});
Adapter内构造外部引用的方法
eg.列表item点击事件:
private OnItemClickListener mItemClickListener;
public interface OnItemClickListener {
void onItemClick(View view, int position);
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.mItemClickListener = onItemClickListener;
}
if (mItemClickListener != null) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// 可以将想要的内容传过去
mItemClickListener.onItemClick(holder.itemView, position);
}
});
}
PopupWindow
当设置触摸空白部分不退出pop的时候,按手机返回键也不能退出pop,可添加以下代码实现按键返回。但是按键返回一次之后,pop弹不出来了
popupWindow = new PopupWindow(popupWindow_view, 200, 150, true);
// 一般都需要让popupWindow里面的组件获得焦点,最后一个字段设为true,也可以代码设置
popupWindow.setFocusable(true); // 用来接收用户输入或操作动作
// 注意: 必须加入下面这行作用未知的语句才能发挥作用:
popupWindow.setBackgroundDrawable(newBitmapDrawable());// 响应返回键必须的语句。
// 设置 BackgroundDrawable 不会改变配置文件中设置的背景颜色或图像。
金额格式(2,018.00)
public static String NumberFormat(double num) {
NumberFormat number_format = NumberFormat.getInstance(Locale.CHINA);
number_format.setMinimumFractionDigits(2);
return number_format.format(num);
}
ScrollView
取消右侧滚动条:
android:scrollbars="none"
子布局设置“match_parent”无效,不能充满整个ScrollView,解决如下:
android:fillViewport="true"
AS全局检索
Ctrl+Shift+F
禁止fragment滑动销毁的方法:
重写FragmentPagerAdapter内的destroyItem()方法
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
//super.destroyItem(container, position, object);// 注释就不会销毁
}
预加载全部Fragment
mViewPager.setOffscreenPageLimit((int)tabSum);// 确保得到附件数组,加载全部fragment
在手机上通过网页链接打开APP(不传值)
参考:https://www.cnblogs.com/sexintercourse/p/5898242.html
https://blog.csdn.net/c10WTiybQ1Ye3/article/details/78098539
网页内链接:<a href="my://test.com/">打开app</a>
这样在网页内点击打开app,当手机上已安装该应用的时候就会启动该APP进入Welcome_Aty页
需要注意的是,如果A、B两段要同时存在同一个Activity时,两个<intent-filter>一定要分开,不然应用图标将消失
每天补充一点点..