text 作为通信者的基本接口

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了text 作为通信者的基本接口相关的知识,希望对你有一定的参考价值。

   		BASIC INTERFACE AS A COMMUNICATOR

How to create an interface:

 public interface ItemClickListener {
    void onItemClick(String message);
}

An Interface has two User class.
Sender: 
who send data through the interface;
How  to use:
1.	First of all declare interface as member variable in the sender class.
	private ItemClickListener mItemClickListener;

2.	Create a setter for this member variable. So that receiver can access the same listener

public void setItemClickListener(ItemClickListener itemClickListener){
    this.mItemClickListener=itemClickListener;
}


3.	Now for sending data:
		
String msg="hello";
mItemClickListener.onItemClick(msg);







Reciever
How to use:

We can use an interface as a receiver in several way.

Way 1 implementing:


class reciever implements ItemClickListener{
    
//that will force you to override the interface methods
}

Create an object of Interface:
private sender mSender;

on any method like main or onCreate initialize the object
void anyMethod(){
    mSender=new sender();

}

now use the setter from the sender to set the interface;
void anyMethod(){
    mSender=new sender();
    mSender.setItemClickListener(this);
}

	
After implementing:
class reciever implements ItemClickListener{

    @Override
    public void onItemClick(String message) {
        
    }
}



Now we can use the variable :
class reciever implements ItemClickListener{

    @Override
    public void onItemClick(String message) {
        Toast.makeText(mContext, ""+ message, Toast.LENGTH_SHORT).show();
    }
}

output:
hello


way 2 :


Without implementing interface on class.

1.	First we need to create an object of sender class
			
private sender mSender;

void anyMethod(){
mSender=new sender();
}



2.	Now use object of using setter.
 
void anyMethod(){
  
    mSender=new sender();
 
    mSender.setItemClickListener(new ItemClickListener() {
        @Override
        public void onItemClick(String message) {
            Toast.makeText(mContext, ""+message, Toast.LENGTH_SHORT).show();
        }
    });
}


以上是关于text 作为通信者的基本接口的主要内容,如果未能解决你的问题,请参考以下文章

java并发

MAC泛洪攻击

text 一个基本的.vimrc文件,它将作为构建的良好模板。

如何在 Python 中直接获取字典键作为变量(而不是通过从值中搜索)?

语音通信语音通信系统包括语音硬件采样,抽样量化,PCM编码解码模块,FIR滤波,QPSK调制解调模块,语音增强模块以及语音信号还原

迪菲.赫尔曼(Diffie–Hellman)密钥交换算法