android开发的小白笔记

第一次android开发功能,遇到了很多问题,也学习到了不少,小白笔记,从现在开始~~

1. 监听键盘收起:

ps. edittext控件,常常获取不到失去焦点事件,于是只能通过对软键盘的监听,实现监听失去焦点事件。

.class文件

privateViewTreeObserver.OnGlobalLayoutListenerkeyboardLayoutListener=newViewTreeObserver.OnGlobalLayoutListener() {

@Override

public voidonGlobalLayout() {

View contentView =getWindow().getDecorView().findViewById(android.R.id.content);

View rootView =contentView.getRootView();

intheightDiff = rootView.getHeight() -contentView.getHeight();

intcontentViewTop =

getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();

if(heightDiff <= contentViewTop) {

keyboarkFocus=false;

priceNumView.clearFocus();

LogUtil.d("zhiyao","clearFocus");

}else if(heightDiff > contentViewTop){

keyboarkFocus=true;

}

}

};

AndroidManifest.xml

android:windowSoftInputMode="adjustResize"


2. 代码中自定义控件高度(为实现控件居中功能)

一般设置控件为居中布局,只需要在xml文件中设置:

android:layout_centerInParent="true"

然而有时,activity设置windowsoftinputmode的时候,布局会被打乱,需要计算屏幕的高度和宽度,进行动态的居中布局。

a. 获取屏幕的宽度或高度

DisplayMetrics metric =newDisplayMetrics();

getWindowManager().getDefaultDisplay().getMetrics(metric);

intwidth = metric.widthPixels;//屏幕宽度(像素)

intheight = metric.heightPixels;//屏幕高度(像素)

floatdensity = metric.density;//屏幕密度(0.75 / 1.0 / 1.5)

intdensityDpi = metric.densityDpi;//屏幕密度DPI(120 / 160 / 240)

b. onCreate()方法中,设置布局

RelativeLayout.LayoutParams layoutParams=newRelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

layoutParams.topMargin= (int) marginTop;

tagLayout.setLayoutParams(layoutParams);

3. 输入框,价格规则的限制

输入价格时,需要将键盘设置为数字键盘。android:inputType="numberDecimal"

价格的规则,比如不能输入多个“.”,小数点最多只能输入两位,第二位不能输入0等

a.设置edittext的输入框监听事件

editTextView.addTextChangedListener(mEditTextWatcher);

b.设置价格规则匹配

//价格控制

privateTextWatchermPriceTextWatcher=newTextWatcher() {

@Override

public voidbeforeTextChanged(CharSequence s,intstart,intcount,intafter) {

}

@Override

public voidonTextChanged(CharSequence s,intstart,intbefore,intcount) {

if(s.toString().contains(".")) {

if(s.length() -1- s.toString().indexOf(".") >2) {

s = s.toString().subSequence(0,

s.toString().indexOf(".") +3);

editTextView.setText(s);

editTextView.setSelection(s.length());

}else if(s.length() -1- s.toString().indexOf(".") ==2&& s.toString().endsWith("0")) {

editTextView.setText(s.subSequence(0, s.length() -1));

editTextView.setSelection(s.length() -1);

}

}

if(s.toString().trim().substring(0).equals(".")) {

s ="0"+ s;

editTextView.setText(s);

editTextView.setSelection(2);

}

if(s.toString().startsWith("0")

&& s.toString().trim().length() >1) {

if(!s.toString().substring(1,2).equals(".")) {

editTextView.setText(s.subSequence(0,1));

editTextView.setSelection(1);

}

}

}

@Override

public voidafterTextChanged(Editable s) {

}

};

4. 可爱的软键盘初始时不显示光标

刚刚进入页面时,默认edittext控件上不显示光标。为解决,需要将焦点事件,放在其他的控件或layout上。我的做法是将其放在了整个页面的layout上。

xml文件中,配置:

android:focusable="true"

android:focusableInTouchMode="true"

ps。遇到了一个很奇怪的坑坑。将以上代码放在控件所在的parent layout,返回该页面时,页面都懂一下。初步定位,是软键盘收起事件导致的,收起键盘后再返回此页面,无类似问题。而且,若该页面曾唤起过软键盘,则不会出现此现象。后来,将以上代码,移动至最外层的一个layout,则问题自愈。

5. 页面布局的技能get

a。扩大热区的方法1

控件的width和height设置的稍微大一点。再使用padding属性。可用于imageview,textview等。

b。edittext或textview 设置一行且字数多了显示“...”

android:maxLines="1"

android:singleLine="true"//maxlines和singleline在魅族手机上需要同时使用

android:ellipsize="end"  //end:"..."在结尾,middle:"..."在中间

c.设置edittext的光标

android:textCursorDrawable="@drawable/edit_icon_style"

d。sp与dp的区别

一般在设置字体的大小时,使用sp。sp会随着系统设置字体的大小而变化。

e。自制空间样式&&不同按下态

<selector>

<item android:state_selected="true">

<shape><corners android:radius="40dp"/>

<solid android:color="@color/white"/>

</shape></item>

</selector>


6. 防止页面唤起多次

singletop && addFlags

androidmanifest:android:launchMode="singleTop"

activity中:intentCity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

a. 如果已经启动了四个Activity:A,B,C和D。在D Activity里,我们要跳到B Activity,同时希望C finish掉,可以在startActivity(intent)里的intent里添加flags标记,如下所示:

Intent intent =newIntent(this, B.class);

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

startActivity(intent);


b.如果已经启动了四个Activity:A,B,C和D,在D Activity里,想再启动一个Actvity B,但不变成A,B,C,D,B,而是希望是A,C,D,B,则可以像下面写代码:

Intent intent =newIntent(this, MainActivity.class);

intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

startActivity(intent);

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容