我们开发的是视频电话,所以既可以视频通话,可以只有音频的通话,所以底部含有两个按钮,最后一个就是删除功能,如果输入错误,我们可以删除输入的内容。
这里我们要通过重写LinearLayout来实现这部份,对应着上面的功能我们可以写一个接口来实现这些功能,如下:
public interface OnDialActionListener {
/**
* The make call button has been pressed
*/
void placeCall(); /**
* The video button has been pressed
*/
void placeVideoCall();
/**
* The delete button has been pressed
*/
void deleteChar();
/**
* The delete button has been long pressed
*/
void deleteAll();
}
通过回调来在主界面实现这些功能。例如在OnClick函数中
@Override
public void onClick(View v) {
if (actionListener != null) {
int viewId = v.getId();
if (viewId == R.id.dialVideoButton) {
actionListener.placeVideoCall();
}else if(viewId == R.id.dialButton) {
actionListener.placeCall();
}else if(viewId == R.id.deleteButton) {
actionListener.deleteChar();
}
}
}
我们可以通过本地的接口来实现内部函数,然后在主界面实例化这个接口并填写接口的实现方式。整体的代码如下:
package com.jwzhangjie.pjsip.widgets; import com.jwzhangjie.pjsip.R; import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.LinearLayout; public class DialerCallBar extends LinearLayout implements OnClickListener, OnLongClickListener { public interface OnDialActionListener {
/**
* The make call button has been pressed
*/
void placeCall(); /**
* The video button has been pressed
*/
void placeVideoCall();
/**
* The delete button has been pressed
*/
void deleteChar();
/**
* The delete button has been long pressed
*/
void deleteAll();
} private OnDialActionListener actionListener; public DialerCallBar(Context context) {
this(context, null, 0);
} public DialerCallBar(Context context, AttributeSet attrs) {
this(context, attrs, 0);
} public DialerCallBar(Context context, AttributeSet attrs, int style) {
super(context, attrs);
LayoutInflater inflater = LayoutInflater.from(context);
inflater.inflate(R.layout.dialpad_additional_buttons, this, true);
findViewById(R.id.dialVideoButton).setOnClickListener(this);
findViewById(R.id.dialButton).setOnClickListener(this);
findViewById(R.id.deleteButton).setOnClickListener(this);
findViewById(R.id.deleteButton).setOnLongClickListener(this); if(getOrientation() == LinearLayout.VERTICAL) {
LayoutParams lp;
for(int i=0; i < getChildCount(); i++) {
lp = (LayoutParams) getChildAt(i).getLayoutParams();
int w = lp.width;
lp.width = lp.height;
lp.height = w;
lp.gravity = Gravity.CENTER_HORIZONTAL;
// Added for clarity but not necessary
getChildAt(i).setLayoutParams(lp); }
}
} /**
* Set a listener for this widget actions
* @param l the listener called back when some user action is done on this widget
*/
public void setOnDialActionListener(OnDialActionListener l) {
actionListener = l;
} /**
* Set the action buttons enabled or not
*/
public void setEnabled(boolean enabled) {
findViewById(R.id.dialButton).setEnabled(enabled);
findViewById(R.id.dialVideoButton).setEnabled(enabled);
findViewById(R.id.deleteButton).setEnabled(enabled);
} @Override
public void onClick(View v) {
if (actionListener != null) {
int viewId = v.getId();
if (viewId == R.id.dialVideoButton) {
actionListener.placeVideoCall();
}else if(viewId == R.id.dialButton) {
actionListener.placeCall();
}else if(viewId == R.id.deleteButton) {
actionListener.deleteChar();
}
}
} @Override
public boolean onLongClick(View v) {
if (actionListener != null) {
int viewId = v.getId();
if(viewId == R.id.deleteButton) {
actionListener.deleteAll();
v.setPressed(false);
return true;
}
}
return false;
} }