Android开发——Android杂乱整理

本文详细介绍了Android中常用的UI控件及其属性,包括SeekBar、MotionEvent、AutoCompleteTextView等,并探讨了手势识别器、PopupWindow的使用方法及桌面快捷方式的创建。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.Seekbar类

Public Methods
void setOnSeekBarChangeListener( SeekBar.OnSeekBarChangeListener l)
设置该组件进度改变时的监听事件
synchronized int getMax()

从ProgressBar继承的方法,获取进度的最大值

synchronized int getProgress()

从ProgressBar继承的方法,获取当前的进度值

synchronized void setMax(int max)

从ProgressBar继承的方法,设置最大值

synchronized void setProgress(int progress)

从ProgressBar继承的方法,设置当前进度

二.MotionEvent终类

Constants
int ACTION_UP 鼠标的抬起事件
int ACTION_DOWN 鼠标的按下事件
int ACTION_MOVE 鼠标的移动事件
Public Methods
final int getAction()
获取当前触发的事件值,可以成员字段值进行判断
final float getX()
获取x坐标值,onTouchEvent谁响应的就是以谁为基准
final float getY()
获取y坐标值,onTouchEvent谁响应的就是以谁为基准
final float getRawX()
获取原生的x坐标值,以整个屏幕为基准
final float getRawY()
获取原生的y坐标值,以整个屏幕为基准

三.AutoCompleteTextView类

说明:这是一个自动提示相关文件的TextView,当你输入某些单词的开头字母时自动提示你相应单词

 <AutoCompleteTextView
        android:id="@+id/actv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:completionThreshold="1"
         />
AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.actv);
		
String[] stringArray = {"狠","恨他","很是问题","我","我们","a123fasdfa","abcfasd"};
		
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,stringArray);
		
actv.setAdapter(adapter);

四.TextView布局特殊属性

<TextView    
    android:shadowColor="#F00"----------阴影颜色    
    android:shadowDx="2"--------- x轴偏移量    
    android:shadowDy="2"--------- y轴偏移量    
    android:shadowRadius="1"----------阴影半径(扩散,羽化半径)    
  
    android:ellipsize="marquee"----------start:点在最开始;middle:点在正中央;end:点在最后边;marquee:走马灯效果,不过需要获取焦点效果才能实现  
    android:singleLine="true"---------始终显示一行  
    android:focusable="true"---------获取焦点  
    android:focusableInTouchMode="true"------------触摸时获取焦点  

	android:drawableLeft="@android:drawable/star_big_on"-------在本文左端添加一个星星的图案,可以选择文字上下所有添加图片
	android:drawablePadding="10dp"----------图片与文字之间的间距

	android:clickable="true"---------让不可点击的控件具备点击效果,与onClick()搭配使用,如果代码中直接setOnClickListener()不用加这个属性也能点击
	android:inputType="textPassword"----------输入数据的类型,phone:电话;textPassword:密码
    android:textAllCaps="true"——————————设置字母全部大写
/>

五.GridView

<GridView
	android:verticalSpacing="10dp"---------GridView行间距
	android:numColumns="3"---------GridView列数
/>

六.CheckBox

注:这样设置可以让控件不会处理任何事件

<CheckBox
	android:clickable="false"--------不可点击
	android:focusable="false"--------不可获取焦点
	android:focusableInTouchMode="false" ----------触摸也不可获取焦点
/>

七.ListView

问题:当ListView的Item中有Button、ImageButton、Checkbox等就会抢占我们的焦点,导致Item被点击没反应

解决:对ListView所在根布局layout设置一个属性【android:descendantFocusability="blocksDescendants"】意思根布局后裔中的控件只作用于它所处的块状区域内,不影响整个ListView的条目

<ListView 
	android:divider="@drawable/ic_launcher" ---------设置条目分割线引用,值可以设置为【@null】
	android:dividerHeight="2dp"-----------设置条目分割线高度
	android:background="@drawable/down_arrow"-----------设置条目背景图片,不过需要与下边cacheColorHint属性连用
	android:cacheColorHint="@android:color/transparent"------------在设置背景图片后,需要设置这个属性值为透明色,ListView滑动的黑色背景可以靠此属性去掉
	android:listSelector="@drawable/transparent"------------此属性可设置点中条目的颜色,不同手机选中ListView条目不做处理的话效果是不同的
	android:scrollbars="none"-------------设置没有滚动条
	>
</ListView>

八.RadioButton

<RadioButton
	android:button="@null"------------去掉按钮的点击效果,立体效果
	android:drawableTop="@drawable/bottom_govaffairs_tab_bg"--------------可以设置图片所在的位置
/>

九.ImageView

<ImageView
	android:scaleType="..."-----------fitXY拉伸前景和背景一样大、centerInside居中显示,会显示出背景、centerCop中间按照背景比例截取一块
	android:src="..."----------这是指前景
	android:background="..."------------这是指背景
/>

十.手势识别器

1.GestureDetector类

Public Constructors
GestureDetector( Context context,  GestureDetector.OnGestureListener listener)
参数1:上下文;参数2:手势监听器
Public Methods
boolean onTouchEvent( MotionEvent ev)
想让手势识别器被启用,需要把控件的MontionEvent事件传入这个方法,委托手势识别器处理手势事件

2.GestureDetector.SimpleOnGestureListener类

Public Methods
boolean onFling( MotionEvent e1,  MotionEvent e2, float velocityX, float velocityY)
参数1:起始点的事件;参数2:结束点的事件;参数3:x方向的速度;参数4:y方向的速度

十一.Vibrator抽象类

注:实例化该对象通过系统服务--getSystemService(VIBRATOR_SERVICE)

Public Methods
abstract void vibrate(long[] pattern, int repeat)
震动,参数1:数组中奇数位数字表示等待的时长,偶数为数字表示震动的时长;参数2:是否重复,-1表示不重复,0表示重复并且从数组0的位置。1表示数组1的位置,并且数组长度变成了从1开始,所以1这是编程的奇数位
abstract void vibrate(long milliseconds)
震动多长时间,参数1:震动的时长

十二.PopupWindow类

Public Constructors
PopupWindow( View contentView, int width, int height, boolean focusable)

初始化弹窗,参数1:填充控件的布局view;参数2:弹窗宽度;参数3:弹窗高度;参数4:是否获取焦点。如果填充的布局没有背景,并且弹窗获取了焦点,那么会造成所有事件会被弹窗处理掉,点返回键和点击界面没有任何反应。所以需要为填充的布局添加一个背景,必须添加,有时可以添加new ColorDrawable();

Public Methods
void showAsDropDown( View anchor, int xoff, int yoff)

显示在指定控件的正下方。参数1:相对的控件;参数2和3:现在弹窗在指定控件正下发时为(0,0)点,这两个参数表示相对这个(0,0)点移动的偏移量

void showAtLocation( View parent, int gravity, int x, int y)

显示的位置。参数1:相对的父控件;参数2:相对父控件的重心位置;参数3和4:相对于重心的距离

void setBackgroundDrawable( Drawable background)

设置背景,并且必须设置背景,否则弹窗将会抢去全部事件

void dismiss()

让弹窗消失的方法

void setOutsideTouchable(boolean touchable)

当点击PopupWindow以外区域时,弹框自动消失

十三.建立桌面快捷方式

(1)Android建立一个快捷方式主要是看Launcher2这个桌面的应用,你去查看这个应用源码的清单文件就会发现以下广播接收者

<receiver
	android:name="com.android.launcher2.InstallShortcutReceiver"
	android:permission="com.android.launcher.permission.INSTALL_SHORTCUT">
	<intent-filter>
		<action android:name="com.android.launcher.action.INSTALL_SHORTCUT" />
	</intent-filter>
</receiver>

(2)所以我们要建立一个快捷方式要通过发送一个广播来实现。

明确:我们是通过发送一个广播来让桌面添加一个快捷方式,所以快捷方式是属于Launcher2这个应用的,不属于我们自己的应用

(3)通过以下代码发送一个广播

Intent intent = new Intent();
//安装快捷方式广播事件
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
//设置快捷方式的图标
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, BitmapFactory.decodeResource(getResources(), R.drawable.drag));
//设置快捷方式的名字
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "我的快捷方式图标");
//设置快捷方式启动的Intent
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, actionIntent);
sendBroadcast(intent);
//创建快捷方式需要权限的支持

十四.TrafficStats类

知识点:

(1)【proc】==>【uid-stat】,有一堆用用户id命名的文件夹,手机安装apk后,系统自动分配给app一个uid,这就是用户id。【proc】==>【uid-stat】,有一堆用用户id命名的文件夹,手机安装apk后,系统自动分配给app一个uid,这就是用户id

(2)因为每次重启手机这个数据都会被清零,所以我们要对上次的数据进行存储,并且对所有存储的数据进行的累加

问题:如何实现流量拦截?

<1>.在Linux系统下,有一个iptable,能将指定的网络请求强制其指向本地

<2>.android下有一款框架可以实现流量拦截————droidwall(需要root权限)

Public Methods
static long getMobileRxBytes()
获取手机下载所消耗的流量(2g/3g)
static long getMobileTxBytes()
获取手机上传所消耗的流量(2g/3g)
static long getTotalRxBytes()
获取手机下载所消耗的总流量(2g/3g+wifi)
tatic long getTotalTxBytes()
获取手机上传所消耗的总流量(2g/3g+wifi)
static long getUidRxBytes(int uid)
获取具体哪个应用下载所消耗的流量(2g/3g+wifi)
static long getUidTcpRxBytes(int uid)
获取具体哪个应用上传所消耗的流量(2g/3g+wifi)

十五.SlidingDrawer

注:控制抽屉的高度或者宽度,通过让其他控件来占位,这样来挤小抽屉

<SlidingDrawer
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	android:content="@+id/content"-----------抽屉中的内容
	android:handle="@+id/handle"-----------抽屉的把手
	android:orientation="horizontal"-----------抽屉放置位置水平或者垂直
	 >

	<ImageView
		android:id="@id/handle"---------引用抽屉的把手
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:src="@drawable/lock" />

	<LinearLayout
		android:layout_width="match_parent"
		android:layout_height="match_parent"
		android:gravity="center"
		android:orientation="ve rtical" >

		<TextView
			android:id="@id/content"---------引用抽屉的内容
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:text="我的第一个抽屉" />
	</LinearLayout>
</SlidingDrawer>

十六.WebView类

Public Methods
void loadUrl( String url)
加载指定url地址的html文件
void setWebViewClient( WebViewClient client)
设置一个WebView是否加载完毕的监听
WebSettings getSettings()
获取WebView中的配置信息的对象,可修改WebView中的字体大小

十七.其他控件

(1)Button有ImageButton区别在于ImageButton没有text属性

(2)ToggleButton:Android原生的开关按钮

(3)AnalogClock:Android原生的钟表控件

(4)DigitalClock:电子数字钟表

(5)DatePicker:Android原生日期选择的控件

(6)TimePicker:Android原生时间选择的控件


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值