Android项目开发实战常用知识点
一:启动页延时两秒再跳转到主界面:
private class SlpashRunnable implements Runnable {
@Override
public void run() {
Log.e("tag: ", "-----------------------");
if (preferences.getBoolean("firstStart", true)) {
SwitchAdTask switchAdTask = new SwitchAdTask(
SlpashActivity.this);
switchAdTask.execute();
} else {
jumpHome();
}
}
}
new Handler().postDelayed(new SlpashRunnable(), 2000);
二:用SharedPreferences判断应用是否第一次登陆:
public class MainActivity extends Activity {
private SharedPreferences spf;
private Boolean b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spf = getSharedPreferences(Common.PACKANG_NAME, MODE_PRIVATE);
b = spf.getBoolean("firstStart", true);
if(b){
Log.e("tag: ", "first");
spf.edit().putBoolean("firstStart", false).commit();
}else{
Log.e("tag: ", "second+");
}
}
}
三:TextView选中变色
1:在color文件夹里新建selector文件,这里取名text_tab_background.xml
2:设置selector资源文件:
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="#C32F30" /><!-- 选择时的颜色 -->
<item android:state_focused="true" android:color="#00FFFF" /><!-- 获得焦点是颜色 -->
<item android:state_pressed="true" android:color="#FFff00" /><!-- 点击是的颜色 -->
<item android:color="#a0a0a2"/><!-- 默认时的颜色 -->
</selector>
3:定义组件
<TextView
android:id="@+id/tv_tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:layout_marginBottom="3dp"
android:singleLine="true"
android:textColor="@color/text_tab_background"
android:textSize="13sp" />
四:Button设置变色:
1:在drawable文件夹里新建selector文件,这里取名item_select.xml
2:分别制造两张同等类型不同颜色的九妹.9.png背景图
3:设置selector资源文件:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/z_2"
<item android:drawable="@drawable/z_1"
android:state_focused="false" android:state_pressed="false"/>
<item android:drawable="@drawable/z_2" android:state_focused="true"/>
<item android:drawable="@drawable/z_1" android:state_focused="false"/>
</selector>
4:定义组件:
<Button
android:id="@+id/but"
android:layout_width="80dp"
android:layout_height="40dp"
android:text="点击变色"
android:background="@drawable/item_select"/>
五:未ImageView设置旋转动画(循环旋转):
1:在res/anim文件夹下新建rotate旋转动画文件,share_anim.xml
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromDegrees="0"
android:interpolator="@android:anim/linear_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:toDegrees="+360" />
2:设置布局:
<ImageView
android:id="@+id/shareImage"
android:pivotX="50%"
android:pivotY="50%"
android:layout_width="47dp"
android:layout_height="47dp"
android:layout_gravity="center_vertical"
android:src="@drawable/sy_tb_4" />
3:在Activity文件中绑定动画:
setRotate(this,shareImage);
public void setRotate(Context mcontext,ImageView image){
Animation anim = AnimationUtils.loadAnimation(mcontext,
R.anim.share_anim);
LinearInterpolator lir = new LinearInterpolator();
anim.setInterpolator(lir);
image.startAnimation(anim);
}
六:字符串:
6.1字符串格式化:
String toast = String.format("%s%d", "您的当前总积分为:", allScore);
七:设置控件的隐藏,显示,不可见和不可点击等状态
android:visibility="visible"
android:visibility="invisible"
android:visibility="gone"
android:enabled="false"
android:editable="false"
android:ems="10"
android:ellipsize="none"