Android 简单回调示例

阳光穿透心脏的1/2处 2022-07-18 06:15 310阅读 0赞

需要完成效果:点击第一个界面文字跳到第二个界面,点击第二个界面按钮跳回到第一个界面,

同时第一个界面的文字发生改变。

第一个界面:

布局:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. tools:context="com.example.administrator.jreduch08.OneActivity">
  7. <TextView
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:text="等待结果"
  11. android:textSize="20sp"
  12. android:gravity="center"
  13. android:id="@+id/tv"
  14. android:clickable="true"/>
  15. </RelativeLayout>

代码:

  1. package com.example.administrator.jreduch08;
  2. import android.content.Intent;
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.TextView;
  7. public class OneActivity extends AppCompatActivity implements TwoActivity.CallBack{
  8. private TextView tv;
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.activity_one);
  13. tv= (TextView) findViewById(R.id.tv);
  14. TwoActivity.setCallBack(this);
  15. tv.setOnClickListener(new View.OnClickListener() {
  16. @Override
  17. public void onClick(View v) {
  18. Intent intent=new Intent(getBaseContext(),TwoActivity.class);
  19. startActivity(intent);
  20. }
  21. });
  22. }
  23. @Override
  24. public void changeText(String resultStr) {
  25. tv.setText(resultStr);
  26. }
  27. }

第二个界面:

布局:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. tools:context="com.example.administrator.jreduch08.TwoActivity">
  7. <Button
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:text="返回结果"
  11. android:id="@+id/back"/>
  12. </RelativeLayout>

代码:

  1. package com.example.administrator.jreduch08;
  2. import android.content.Context;
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.Button;
  7. public class TwoActivity extends AppCompatActivity {
  8. private Button back;
  9. private static CallBack callBack;
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_two);
  14. back= (Button) findViewById(R.id.back);
  15. back.setOnClickListener(new View.OnClickListener() {
  16. @Override
  17. public void onClick(View v) {
  18. callBack.changeText("这就是回调,厉害了");
  19. finish();
  20. }
  21. });
  22. }
  23. public static void setCallBack(Context context){
  24. callBack= (CallBack) context;
  25. }
  26. public interface CallBack{
  27. public void changeText(String resultStr);
  28. }
  29. }

完成效果:

**Center

Center 1
Center 2**

发表评论

表情:
评论列表 (有 0 条评论,310人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Android(callback)的简单用法

    接触Android这么久,老是听到回调这个词,觉得很高大上,一直很想弄明白这到底是个什么鬼东东,同事只用了简简单单的一句话就让我有了很直观的理解:一个监听器就是一个回调方法的实

    相关 android中的

         回调这种思想大家应该都用过,只是很多人不知道那是回调的一种罢了,前几天整理了一下自己对于回调的理解,就顺便把自己的一些想法整理到博客中。   让我们从一个

    相关 android函数总结

     回调函数就是那些自己写的,但是不是自己来调,而是给别人来掉的函数。 消息响应函数就可以看成是回调函数,因为是让系统在合适的时候去调用。这不过消息响应函