Acitivty
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView tv_info;
private Button bt_back;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt_back = (Button) findViewById(R.id.bt_back);
tv_info = (TextView) findViewById(R.id.tv_info);
Date date = new Date(null, new Info() {
@Override
public void date(int color) {
bt_back.setBackgroundResource(color);
}
});
}
}
实体类:
/**
* Created by Qings on 2016/8/9.
*/
public class Date {
public Date(String str ,Info info){
if (str != null){
info.date(R.color.colorPrimaryDark);
}
}
}
//接口
/**
* Created by Qings on 2016/8/9.
*/
public interface Info {
public void date(int color);
}
xml布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="com.example.qings.myapplication.MainActivity">
<TextView
android:layout_marginTop="50dp"
android:id="@+id/tv_info"
android:layout_width="match_parent"
android:layout_height="50dp" />
<Button
android:id="@+id/bt_back"
android:layout_below="@id/tv_info"
android:layout_width="match_parent"
android:text="Button"
android:layout_height="wrap_content" />
</RelativeLayout>
ps:接口当做一个特殊的类对待!
这样就更容易理解了
来源:oschina
链接:https://my.oschina.net/u/2355512/blog/540359