安卓开发-Activity之间的交互

内容纲要

Activity之间的交互

目的

通过点击MainActivity中的btncheck按钮,将数据传给SecondaryActivity,同时SecondaryActivity接收数据再回传数据,MainActivity对回传的数据进行处理,实现两个Activity之间互动。

显式意图和隐式意图的区别

概念

  1. 显式意图:显式意图明确指明了启动活动的上下文和想要启动的目标活动,显式意图明确指定了Intent应该传递给哪个组件。
  2. 隐式意图:没有明确指定组件名的Intent为隐式意图。 Android系统会根据隐式意图中设置的动作(action)、类别(category)、数据(URI和数据类型)找到最合适的组件来处理这个意图。

点击查看官方关于Intent的文档

具体代码

隐式意图

  1. 通过SecondaryActivity的action找到最合适的组件

    activity android:name=".SecondaryActivity">
        intent-filter>
           action android:name="MyChange"/>
           category android:name="android.intent.category.DEFAULT"/>
        /intent-filter>
    /activity>
  2. 设定Intent的动作并启用

    intent.setAction("MyChange");
    startActivity(intent);
    //startActivityForResult(intent,1);   //带请求代码

显式意图

明确指定Intent的目标组件

    Intent intent = new Intent(this,SecondActivity.class);
    startActivity(intent);

意图的使用

  1. 发送意图

        Intent intent = new Intent();
        String message = editText.getText().toString();
        intent.putExtra("etmessage",message);
        startActivityForResult(intent,1);   //带请求代码
  2. 接收意图

        Intent intent = getIntent();
        messageReceive = intent.getStringExtra("etmessage"); textView.setText(messageReceive);
  3. 回传数据

        Intent intent_back = new Intent();
        intent_back.putExtra("result",flag);
        setResult(1,intent_back);   //回传代码
        finish();
  4. 接收回传数据

        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            // TODO Auto-generated method stub
            super.onActivityResult(requestCode, resultCode, data);
            if(requestCode == 1 && resultCode == 1){    //请求码和结果码相匹配
                EditText editText = findViewById(R.id.etFreedom);
                boolean ret = data.getBooleanExtra("result",false);
                if(ret){
                    //editText.setText("说谎了");
                    yesDialog();
                }else{
                //editText.setText("没说谎");
                    noDialog();
                }
            }

自定义对话框

过程
1.创建一个AlertDialog.builder
2.创建一个View,将自定义的xml放入view中
3.将view放入builder中
4.创建一个Dialog,并将builder联系起来
5.最后用show方法展示出来

 private void yesDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        // 创建一个view,并且将布局加入view中
        View view = LayoutInflater.from(MainActivity.this).inflate(
                R.layout.imageitem, null, false);
        // 将view添加到builder中
        builder.setView(view)
                .setPositiveButton("关闭",null);
        // 创建dialog
        final Dialog dialog = builder.create();
        dialog.show();
    }

综合代码

manifests.xml

 <activity android:name=".SecondaryActivity">
            <intent-filter>
                <action android:name="MyChange"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void sendMessage(View view){
        switch (view.getId()){
            case R.id.btnCheck:
                Intent intent = new Intent();
                intent.setAction("MyChange");
                EditText editText = findViewById(R.id.etFreedom);
                String message = editText.getText().toString();
                intent.putExtra("etmessage",message);
                startActivityForResult(intent,1);
        }
    }
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 1 && resultCode == 1){
            EditText editText = findViewById(R.id.etFreedom);
            boolean ret = data.getBooleanExtra("result",false);
            if(ret){
                //editText.setText("说谎了");
                yesDialog();
            }else{
//                editText.setText("没说谎");
                noDialog();
            }
        }
    }
    private void yesDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        // 创建一个view,并且将布局加入view中
        View view = LayoutInflater.from(MainActivity.this).inflate(
                R.layout.imageitem, null, false);
        // 将view添加到builder中
        builder.setView(view)
                .setPositiveButton("关闭",null);
        // 创建dialog
        final Dialog dialog = builder.create();
        dialog.show();
    }
    private void noDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        // 创建一个view,并且将布局加入view中
        View view = LayoutInflater.from(MainActivity.this).inflate(
                R.layout.img_secondary, null, false);
        // 将view添加到builder中
        builder.setView(view)
                .setPositiveButton("关闭",null);
        // 创建dialog
        final Dialog dialog = builder.create();
        dialog.show();
    }

}

SecondaryActivity.java

public class SecondaryActivity extends AppCompatActivity {
    String messageReceive;
    boolean flag;
    TextView textView2;
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_secondary);
        textView = findViewById(R.id.tvReceive);
        textView2 = findViewById(R.id.tvCheck);
        Intent intent = getIntent();
        messageReceive = intent.getStringExtra("etmessage");
        textView.setText(messageReceive);
        edit();
    }
    public void btnback(View view){
        Reback();
    }

    public boolean letterCheck(){
        if(messageReceive.contains("谎")) return true;
        else return false;
    }
    public void Reback(){
        flag = letterCheck();
        Intent intent_back = new Intent();
        intent_back.putExtra("result",flag);
        setResult(1,intent_back);
        finish();
    }
    public void edit(){
        if (letterCheck()) textView2.setText("此话经检测,判定你说谎了");
        else textView2.setText("此话经检测,判定你没有说谎");
    }
}

发表评论