1、自定义控件:
虽然Android系统提供了很多系统控件给我们使用,但在实际应用开发中,由于系统控件太过于固定化,很难满足我们的多样化需求,这就需要我们通过自定义控件去满足我们的需求。
2、自定义控件实例详解:
本案列主要实现了软键盘收缩监听,以及底部view扩展性操作;
主类: MainActivity.java:
package com.example.mysentmmsview;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity{
private MySentMmsView MyView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyView = (MySentMmsView)findViewById(R.id.sent_mms);
}
}
自定义view:MySentMmsView.java
通过继承LinearLayout实现自定义操作view,在该类中onMeasure方法中进行软键盘收缩监听。
构造方法是其初始化入口,如:
public MySentMmsView(Context context, AttributeSet attrs)
onMeasure方法:
onMeasure方法在控件的父元素正要放置它的子控件时调用,当它所在的自定义view发生位置改变时,父元素会根据当前空间,传入两个参数——widthMeasureSpec和heightMeasureSpec.
package com.example.mysentmmsview;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
public class MySentMmsView extends LinearLayout implements OnClickListener{
private MainActivity mContext;
private LinearLayout mTabLinely;
private EditText mEdit;
private Button mBtn;
private InputMethodManager imm;
private int preHeight = 0;
private boolean isHeight = false;
private boolean isSmile = false;
private boolean isinitSmile = false;
public MySentMmsView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public MySentMmsView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = (MainActivity) context;
imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
intiSelf(context);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int h = getViewHeight();
if(h!=0 && !isHeight){
preHeight = h;
isHeight = true;
}
if(isHeight){
if(preHeight>h){
mTabLinely.setVisibility(View.GONE);
}else{
if(isinitSmile && isSmile){
mTabLinely.setVisibility(View.VISIBLE);
}
}
}
}
private int getViewHeight(){
Rect outRect = new Rect();
mContext.getWindow().getDecorView()
.getWindowVisibleDisplayFrame(outRect);
//getWindowVisibleDisplayFrame方法可以获取到程序显示的区域,包括标题栏,但不包括状态栏。于是,我们就可以算出状态栏的高度了。
return outRect.height();
}
private void intiSelf(Context context) {
LayoutInflater inflater;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.myview, this);
mTabLinely = (LinearLayout)findViewById(R.id.myview_tab);
mBtn = (Button)findViewById(R.id.myview_btn);
mEdit = (EditText)findViewById(R.id.myview_edit);
mBtn.setOnClickListener(this);
mEdit.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.myview_edit:
isSmile = false;
break;
case R.id.myview_btn:
if(!isSmile){
//当前是底部栏显示
isinitSmile = true;
imm.hideSoftInputFromWindow(mEdit.getWindowToken(), 0); //强制隐藏键
mTabLinely.setVisibility(View.VISIBLE);
isSmile = true;
}else{
//当前是软件盘显示
//imm.hideSoftInputFromWindow(mEdit.getWindowToken(), 0); //强制隐藏键盘
mEdit.requestFocus();
imm.showSoftInput(mEdit,InputMethodManager.SHOW_FORCED);
isSmile = false;
}
break;
}
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- 自定义的底部view -->
<com.example.mysentmmsview.MySentMmsView
android:id="@+id/sent_mms"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
myview.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/myview_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<EditText
android:id="@+id/myview_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</EditText>
</LinearLayout>
<LinearLayout
android:id="@+id/myview_tab"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#000000"
android:orientation="vertical"
android:visibility="gone">
</LinearLayout>
</LinearLayout>