Java 回调
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
for(int i=0; i < 10; i++){
System.out.print(i);
}
////////////////////////////////////
new Thread(new Runnable(){
public void run(){
Test t = new Test();
t.relist(new Test.CallBack(){
public void getdata(List<String> list){
System.out.println(list);
}
});
}
}).start();
///////////////////////////////////
///////////////////////////////////
// Test t = new Test();
// t.relist(new Test.CallBack(){
// public void getdata(List<String> list){
// System.out.println(list);
// }
// });
///////////////////////////////////
for(int i=0; i < 10; i++){
System.out.print(i);
}
}
}
class Test{
private List<String> ls;
public interface CallBack{
public void getdata(List<String> list);
}
public void relist(CallBack calback){
ls = new ArrayList<String>();
for(int i = 0; i< 10; i++){
ls.add("add" + i);
}
calback.getdata(ls);
}
}
https://paiza.io/projects/HaU5z4cE88fPD8xvSeUznA
2016年10月21日金曜日
2016年10月20日木曜日
在MainActivity内调用Fragment内的控件并实现方法
在MainActivity内调用Fragment内的控件,
并在MainActivity内实现Fragment内的控件的方法
MainActivety.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private FragmentManager fragmentManager;
private FragmentTransaction fragmentTransaction;
private xxx x;
private vvv v;
private EditText xet ,vet;
private Button xbt, vbt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentManager = getFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
x = new xxx();
v = new vvv();
fragmentTransaction.add(R.id.vvv, v, "fv");
fragmentTransaction.add(R.id.xxx, x, "fx");
fragmentTransaction.commit();
}
@Override
protected void onStart() {
super.onStart();
xbt = (Button) x.getView().findViewById(R.id.xxxbutton);
vbt = (Button) v.getView().findViewById(R.id.vvvbutton);
xbt.setOnClickListener(this);
vbt.setOnClickListener(this);
}
@Override
public void onClick(View view) {
vet = (EditText) v.getView().findViewById(R.id.vvveditText);
xet = (EditText) x.getView().findViewById(R.id.xxxeditText);
switch(view.getId()){
case R.id.vvvbutton:
if(vet != null) {
vet.setText(xet.getText().toString());
System.out.println(vet);
}
break;
case R.id.xxxbutton:
if(xet != null) {
xet.setText(vet.getText().toString());
System.out.println(xet);
}
break;
default:
break;
}
}
}
并在MainActivity内实现Fragment内的控件的方法
MainActivety.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private FragmentManager fragmentManager;
private FragmentTransaction fragmentTransaction;
private xxx x;
private vvv v;
private EditText xet ,vet;
private Button xbt, vbt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentManager = getFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
x = new xxx();
v = new vvv();
fragmentTransaction.add(R.id.vvv, v, "fv");
fragmentTransaction.add(R.id.xxx, x, "fx");
fragmentTransaction.commit();
}
@Override
protected void onStart() {
super.onStart();
xbt = (Button) x.getView().findViewById(R.id.xxxbutton);
vbt = (Button) v.getView().findViewById(R.id.vvvbutton);
xbt.setOnClickListener(this);
vbt.setOnClickListener(this);
}
@Override
public void onClick(View view) {
vet = (EditText) v.getView().findViewById(R.id.vvveditText);
xet = (EditText) x.getView().findViewById(R.id.xxxeditText);
switch(view.getId()){
case R.id.vvvbutton:
if(vet != null) {
vet.setText(xet.getText().toString());
System.out.println(vet);
}
break;
case R.id.xxxbutton:
if(xet != null) {
xet.setText(vet.getText().toString());
System.out.println(xet);
}
break;
default:
break;
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context="com.example.java.m1020a.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/xxx"
android:orientation="vertical"
></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/vvv"
android:orientation="vertical"
></LinearLayout>
</LinearLayout>
vvv.java
xxx.java
public class vvv extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fvvv, null);
return v;
}
@Override
public void onPause() {
super.onPause();
}
}
fvvv.xml
fxxx.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="fv"
android:ems="10"
android:id="@+id/vvveditText" />
<Button
android:text="fvvv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/vvvbutton" />
</LinearLayout>
2016年10月19日水曜日
在MainActivity 调用 Fragment 的控件
在MainActivity 调用代码方式加载的 Fragment 内的控件。
public class MainActivity extends AppCompatActivity {
private FragmentManager fm;
private FragmentTransaction ft;
private Button bt;
private Fraga fa;
@Override
//加载Fragment
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fm = getFragmentManager();
ft = fm.beginTransaction();
fa = new Fraga();
ft.add(R.id.fraga, fa, "fa");
ft.commit();
}
@Override
protected void onStart() {
super.onStart();
// 获得Fragment的控件
Button bt = (Button) fa.getView().findViewById(R.id.button);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView tv = (TextView) fa.getView().findViewById(R.id.textView);
tv.setText("MainActivity");
}
});
}
}
public class MainActivity extends AppCompatActivity {
private FragmentManager fm;
private FragmentTransaction ft;
private Button bt;
private Fraga fa;
@Override
//加载Fragment
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fm = getFragmentManager();
ft = fm.beginTransaction();
fa = new Fraga();
ft.add(R.id.fraga, fa, "fa");
ft.commit();
}
@Override
protected void onStart() {
super.onStart();
// 获得Fragment的控件
Button bt = (Button) fa.getView().findViewById(R.id.button);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView tv = (TextView) fa.getView().findViewById(R.id.textView);
tv.setText("MainActivity");
}
});
}
}
//如果在onCreate方法中去获取Fragment的控件,可能因Fragment
被加载之前调用 getView 方法,会出空指针错误。
2016年10月18日火曜日
Java 接口回调
Java 接口回调
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws Exception {
for(int l = 0; l < 9 ; l++){
System.out.println(l);
BBQ b = new BBQ();
b.XBBQ(new BBQ.CallBack(){
@Override
public void getstring(int s){
System.out.println("iii->" + s );
}
});
}
}
}
class BBQ{
private int i;
public void XBBQ(CallBack callback){
new Thread(new Runnable(){
public void run(){
for(i=0; i< 99090999; i++){
}
callback.getstring(i);
}
}).start();
}
public interface CallBack{
public void getstring(int s);
}
}
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws Exception {
for(int l = 0; l < 9 ; l++){
System.out.println(l);
BBQ b = new BBQ();
b.XBBQ(new BBQ.CallBack(){
@Override
public void getstring(int s){
System.out.println("iii->" + s );
}
});
}
}
}
class BBQ{
private int i;
public void XBBQ(CallBack callback){
new Thread(new Runnable(){
public void run(){
for(i=0; i< 99090999; i++){
}
callback.getstring(i);
}
}).start();
}
public interface CallBack{
public void getstring(int s);
}
}
https://paiza.io/projects/8cAVqtuJtHQwgWq0mPJZkg
Android 接口回调 和 多线程
Android 接口回调 和 多线程
Back.java
public class Back {
private int i;
public interface CallBack{
public void getstring(int s);
}
public void demo(final CallBack callBack){
Thread tt = new Thread(new Runnable() {
@Override
public void run() {
for (i = 0; i < 999999; i++){
}
callBack.getstring(i);
}
});
try {
tt.sleep(5000);
}catch (Exception e){
}
tt.start();
}
}
Back.java
public class Back {
private int i;
public interface CallBack{
public void getstring(int s);
}
public void demo(final CallBack callBack){
Thread tt = new Thread(new Runnable() {
@Override
public void run() {
for (i = 0; i < 999999; i++){
}
callBack.getstring(i);
}
});
try {
tt.sleep(5000);
}catch (Exception e){
}
tt.start();
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
private EditText ed;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread(new Runnable() {
@Override
public void run() {
Back back = new Back();
back.demo(new Back.CallBack() {
@Override
public void getstring(final int s) {
System.out.println(s);
ed = (EditText) findViewById(R.id.editText);
runOnUiThread(new Runnable() {
@Override
public void run() {
ed.setText("合計:" + s);
}
});
}
});
}
}).start();
}
}
创建对 Activity 的事件回调
创建对 Activity 的事件回调
MainActivity.java
public class MainActivity extends AppCompatActivity {
private FragmentManager fragmentManager;
private FragmentTransaction fragmentTransaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Fraga fraga = new Fraga();
fragmentManager = getFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragment, fraga, "fragment1");
fragmentTransaction.commit();
Button bt = (Button) findViewById(R.id.button);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fraga.setcall(new Fraga.CallBack() {
@Override
public void getText(String xx) {
Toast.makeText(MainActivity.this, xx, Toast.LENGTH_SHORT).show();
}
});
}
});
}
}
Fraga.java
public class Fraga extends Fragment {
EditText editText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fraga, null);
editText = (EditText) v.findViewById(R.id.editText2);
return v;
}
public void setcall(CallBack callBack){
callBack.getText(editText.getText().toString());
}
public interface CallBack{
public void getText(String xx);
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
private FragmentManager fragmentManager;
private FragmentTransaction fragmentTransaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Fraga fraga = new Fraga();
fragmentManager = getFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragment, fraga, "fragment1");
fragmentTransaction.commit();
Button bt = (Button) findViewById(R.id.button);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fraga.setcall(new Fraga.CallBack() {
@Override
public void getText(String xx) {
Toast.makeText(MainActivity.this, xx, Toast.LENGTH_SHORT).show();
}
});
}
});
}
}
Fraga.java
public class Fraga extends Fragment {
EditText editText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fraga, null);
editText = (EditText) v.findViewById(R.id.editText2);
return v;
}
public void setcall(CallBack callBack){
callBack.getText(editText.getText().toString());
}
public interface CallBack{
public void getText(String xx);
}
}
2016年10月17日月曜日
Fragment使用findFragmentById返回null
Fragment使用findFragmentById返回null
findFragmentById 好像只能用在XML内添加的Fragment。如下
<LinearLayout
<fragment
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.example.e560.m1016a.FL"
android:id="@+id/fragment" />
</LinearLayout>
否则只能用 getFragmentManager().findFragmentByTag。
fm = getFragmentManager();
ft = fm.beginTransaction();
FL fl = new FL();
ft.add(R.id.a, fl, "fl");
ft.commit();
FL ff = (FL) getFragmentManager().findFragmentByTag("fl");
FL ff2 = (FL) getFragmentManager().findFragmentById(R.id.fragment);
findFragmentById 好像只能用在XML内添加的Fragment。如下
<LinearLayout
<fragment
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.example.e560.m1016a.FL"
android:id="@+id/fragment" />
</LinearLayout>
否则只能用 getFragmentManager().findFragmentByTag。
fm = getFragmentManager();
ft = fm.beginTransaction();
FL fl = new FL();
ft.add(R.id.a, fl, "fl");
ft.commit();
FL ff = (FL) getFragmentManager().findFragmentByTag("fl");
FL ff2 = (FL) getFragmentManager().findFragmentById(R.id.fragment);
I/System.out: FL{527fd414 #1 id=0x7f0b0055 fl}
I/System.out: FL{5280f1c0 #0 id=0x7f0b0056}
登録:
投稿 (Atom)