zoukankan      html  css  js  c++  java
  • Android_Intent_startActivityForResult

    <LinearLayout 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"
       android:orientation="vertical"
          tools:context=".MainActivity" >
    
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="请输入搜索内容:"
            android:textSize="20sp"
            />
        <EditText 
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:textSize="20sp"/>
        <Button 
            android:id="@+id/btn_search"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="开始搜索"/>
        <ImageView 
            android:id="@+id/iamge"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/ic_launcher"
            />
        
    
    </LinearLayout>

    MainActivity.java

    package com.example.day05_startactivityforresult;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Intent;
    import android.text.TextUtils;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ImageView;
    /**
     * 需求:通过输入搜索的内容查找该内容对应的图片,并显示在页面上
     * 分析:
     * 1.初始控件 初始数据
     * 2.点击搜素
     *     2.1获取搜索内容
     *     2.2存到intent中
     *     2.3开启新的Activity,并传值
     * 3.新的activity
     *     3.1获取传入的值
     *     3.2展示
     *     3.3点击确定按钮
     *         3.3.1将图片地址存起来
     *         3.3.2将值返回给MainActivity
     *         3.3.3关闭当前activity
     *     4.MainActivity获取返回值
     *         4.1判断值是否符合要求
     *         4.2取出值
     *         4.3更新图片
     * @author my
     *
     */
    public class MainActivity extends Activity implements OnClickListener{
    
        private EditText et_input;
        private ImageView image;
        private Button btn_search;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initView();
            btn_search.setOnClickListener(this);
        }
        /**
         * 初始化控件
         */
        private void initView() {
            et_input = (EditText) findViewById(R.id.text);
            image = (ImageView) findViewById(R.id.iamge);
            btn_search = (Button) findViewById(R.id.btn_search);
        }
        /**
         * 按钮单击事件:
         * 点击搜索按钮,跳转页面
         */
        @Override
        public void onClick(View v) {
            //获取搜索内容
            String name = et_input.getText().toString().trim();
            /**
             * 判断输入搜索的内容是否为空
             */
            if(TextUtils.isEmpty(name)){
                return;
            }
            //创建一个Intent对象,并指定要跳转的Activity
            Intent intent = new Intent(MainActivity.this,Activity2.class);
            //将数据存到intent
            intent.putExtra("NAME", name);
            //跳转到新的activity,并设置请求吗
            startActivityForResult(intent, 100);
        }
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if(requestCode == 100 && resultCode == 101){
                int id = data.getIntExtra("IMAGE", -1);
                image.setImageResource(id);
            }
        }
    
    }

    Activity2.java

    package com.example.day05_startactivityforresult;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;
    
    public class Activity2 extends Activity{
        private TextView tv;
        private Button btn_return;
        private String name;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.layout2);
            //初始化控件
            tv = (TextView) findViewById(R.id.tv_show);
            btn_return = (Button) findViewById(R.id.btn_return);
            //获取传递过来的intent
            Intent intent = getIntent();
            name = intent.getStringExtra("NAME");
            tv.setText(name);
            btn_return.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(Activity2.this,MainActivity.class);
                    int id = getData();
                    //将图片的id设置到intent中
                    intent.putExtra("IMAGE", id);
                    //跳转到之前的activity并带结果码
                    setResult(101,intent);
                    //关闭当前活动(很重要!)
                    finish();
                }
                });
        }
    
        private int getData() {
            int returnImgId =R.drawable.ic_launcher; 
            if("deer".equals(name)){
                returnImgId = R.drawable.deer;
            }else if("panda".equals(name)){
                returnImgId = R.drawable.panda;
            }else if("sheep".equals(name)){
                returnImgId = R.drawable.sheep;
            }
            return returnImgId;
        }
    }
  • 相关阅读:
    Mybatis 与 spring mvc
    Extjs 表格横坐标显示问题
    Extjs 图片的自动缩放
    C# Winform 界面中各控件随着窗口大小变化
    spring 连接各种数据源的配置(转载)
    <转>(C#)WinForm窗体间传值
    java stack 底层详细
    java hashmap 底层详细
    java LinkedList 底层详细
    java ArrayList 底层详细
  • 原文地址:https://www.cnblogs.com/fangg/p/5722959.html
Copyright © 2011-2022 走看看