接口的两种作用:
- 监听和回调,在未来的某个时刻执行当前方法。例子:点击事件的监听、网络请求的回调
具体实现:
- 在Class1中定义接口,
- Class1中执行 Class2的setListener(interface的实现)方法,为Class2中的接口赋值,
- 在Class2中合适的地方调用interface的方法
- 传递数据
例子:fragment和activity之间 进行数据传递,Activity实现一个接口,Fragment在onAttach方法中,将该Activity转化为该接口,在需要调用的时候回调。
具体实现:
- 在Class2中定义接口
- 在Class1中为接口赋值,
- 在Class1中调用interface的方法,数据以方法参数的形式传递,
接口用于监听的例子:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context="com.example.interfacedemo.MainActivity" >
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="监听SecondActivity"
/>
</LinearLayout>
/**
* interface 用于监听
* @author car
*
*/
public class MainActivity extends Activity {
com.example.interfacedemo.MRelativeLayout mRelativeLayout;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
Button button = (Button) findViewById(R.id.bt);
textView = (TextView) findViewById(R.id.tv);
mRelativeLayout = (MRelativeLayout) findViewById(R.id.rl);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
start();
Log.d("tag", "MainActivity");
}
});
}
/**
* 启动监听
*/
private void start() {
mRelativeLayout.setListener(new MInterface() {
@Override
public void method() {
textView.setText("监听成功");
}
});
}
}
public interface MInterface {
void method();
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.interfacedemo.MainActivity" >
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<com.example.interfacedemo.MRelativeLayout
android:id="@+id/rl"
android:layout_width="300dip"
android:layout_height="100dip"
android:background="#00ff00"
>
</com.example.interfacedemo.MRelativeLayout>
<Button
android:id="@+id/bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始监听"
/>
</LinearLayout>
public class MRelativeLayout extends RelativeLayout{
private Context context;
private MInterface mInterface;
public MRelativeLayout(Context context) {
super(context);
this.context = context;
init();
}
public MRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context = context;
init();
// TODO Auto-generated constructor stub
}
public MRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
init();
// TODO Auto-generated constructor stub
}
private void init() {
View view = LayoutInflater.from(context).inflate(R.layout.activity_main, null);
addView(view);
Button button = (Button) view.findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
feedBack();
Log.d("tag", "MRelativeLayout");
}
});
}
private void feedBack() {
if (mInterface==null) {
return;
}
mInterface.method();
}
public void setListener(MInterface mInterface) {
this.mInterface = mInterface;
}
}
操作步骤:1,点击 开始监听;2,点击监听SecondActivity。
效果: 第一个Hellow Word 变成 监听成功
传递数据:
/**
* interface 用于传递数据
* @author car
*
*/
public class SecondActivity extends Activity{
MInterface2 mRelativeLayout;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
Button button = (Button) findViewById(R.id.bt);
textView = (TextView) findViewById(R.id.tv);
mRelativeLayout = (MInterface2) findViewById(R.id.rl);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
start();
Log.d("tag", "MainActivity");
}
});
}
/**
* 启动监听
*/
private void start() {
mRelativeLayout.method("传递数据成功");
}
}
public interface MInterface2 {
void method(String string);
}
public class MRelativeLayout2 extends RelativeLayout implements MInterface2{
private Context context;
TextView textView;
public MRelativeLayout2(Context context) {
super(context);
this.context = context;
init();
}
public MRelativeLayout2(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context = context;
init();
// TODO Auto-generated constructor stub
}
public MRelativeLayout2(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
init();
// TODO Auto-generated constructor stub
}
private void init() {
View view = LayoutInflater.from(context).inflate(R.layout.activity_main, null);
textView = (TextView) view.findViewById(R.id.tv);
addView(view);
}
@Override
public void method(String string) {
textView.setText(string);
}
}
点击开始监听:第二个Hellow Word 变成 传递数据成功