咱們開發的是視頻電話,因此既能夠視頻通話,能夠只有音頻的通話,因此底部含有兩個按鈕,最後一個就是刪除功能,若是輸入錯誤,咱們能夠刪除輸入的內容。java
這裏咱們要經過重寫LinearLayout來實現這部份,對應着上面的功能咱們能夠寫一個接口來實現這些功能,以下:android
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函數中ide
@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; } }