Interface 的使用

本文通过两个具体实例介绍接口在Android开发中的应用:一是作为监听机制,实现组件间的事件响应;二是作为数据传递通道,允许不同组件间的数据交换。

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

接口的两种作用:
  • 监听和回调,在未来的某个时刻执行当前方法。例子:点击事件的监听、网络请求的回调
具体实现:
  1. 在Class1中定义接口,
  2. Class1中执行 Class2的setListener(interface的实现)方法,为Class2中的接口赋值,
  3. 在Class2中合适的地方调用interface的方法

  • 传递数据
例子:fragment和activity之间 进行数据传递,Activity实现一个接口,Fragment在onAttach方法中,将该Activity转化为该接口,在需要调用的时候回调。
具体实现:
  1. 在Class2中定义接口
  2. 在Class1中为接口赋值,
  3. 在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 变成 传递数据成功




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值