In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you how to call another Activity in Android and return the results, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!
Realize
The first is the layout of the main page MainActivity, adding a select avatar button and an ImageView to display the avatar.
Then in the OnCreate method in MainActivity, you can use startActivityForResult to start another Activity and get the return result.
To set a request code, this is 200.
Button button = (Button) findViewById (R.id.button); button.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {Intent intent = new Intent (MainActivity.this,HeadActivity.class); startActivityForResult (intent,200);}})
Then jump to the second Activity to select the avatar. First set up its layout file and add a GridView to display the avatar photo you want to select.
Activity_head.xml
Then go to its Activity and use the adapter to set up the photo source for it.
First declare an array of pictures
Private int [] imageId = new int [] {R.drawable.img1, R.drawable.img2}
Here are two avatar photos under drawable.
Then use the adapter to set the data source for the photo
GridView gridView = (GridView) findViewById (R.id.gridView); BaseAdapter adapter = new BaseAdapter () {@ Override public int getCount () {return imageId.length;} @ Override public Object getItem (int position) {return null;} @ Override public long getItemId (int position) {return 0;} @ Override public View getView (int position, View convertView, ViewGroup parent) {ImageView imageView; if (convertView = = null) {imageView = new ImageView (HeadActivity.this); imageView.setAdjustViewBounds (true); imageView.setMaxWidth (158) ImageView.setMaxHeight; imageView.setPadding (5,5,5,5);} else {imageView = (ImageView) convertView;} imageView.setImageResource (imageID [position]); return imageView;}}; gridView.setAdapter (adapter)
Then in the gridView option Click event listener, get the index of the selected photo and return the data through the putInt of the Bundle object and the putExtras of the intent object. Then you call setResult (200); the result is returned, and the request result code is also set to 200.
Complete HeadActivity.java
Package com.badao.selectimage;import androidx.appcompat.app.AppCompatActivity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.BaseAdapter;import android.widget.GridView;import android.widget.ImageView;public class HeadActivity extends AppCompatActivity {private int [] imageId = new int [] {R.drawable.img1PowerR.drawable.img2}; @ Override protected void onCreate (Bundle savedInstanceState) {GridView gridView = (GridView) findViewById (R.id.gridView) BaseAdapter adapter = new BaseAdapter () {@ Override public int getCount () {return imageId.length;} @ Override public Object getItem (int position) {return null;} @ Override public long getItemId (int position) {return 0;} @ Override public View getView (int position, View convertView, ViewGroup parent) {ImageView imageView; if (convertView = = null) {imageView = new ImageView (HeadActivity.this); imageView.setAdjustViewBounds (true); imageView.setMaxWidth (158,150); imageView.setPadding (5,5,5,5) } else {imageView = (ImageView) convertView;} imageView.setImageResource (imageID [position]); return imageView;}}; gridView.setAdapter (adapter); super.onCreate (savedInstanceState); setContentView (R.layout.activity_head); gridView.setOnItemClickListener (new AdapterView.OnItemClickListener () {@ Override public void onItemClick (AdapterView parent, View view, int position, long id) {Intent intent = getIntent (); Bundle bundle = new Bundle (); bundle.putInt ("imageId", imageId [position]); intent.putExtras (bundle); setResult (20058) Finish ();}})
Then go back to how MainActivity accepts the returned results.
Ctrl + O rewriting method onActivityResult. If both the request code and the return result code are 200, first pass the
Bundle bundle = data.getExtras ()
Gets the Bundle object.
Then pass through
Int imageId = bundle.getInt ("imageId")
Gets the returned photo index data.
Then set up a photo source for ImageView.
@ Override protected void onActivityResult (int requestCode, int resultCode, @ Nullable Intent data) {super.onActivityResult (requestCode, resultCode, data); if (requestCode = = data.getExtras (); int imageId = bundle.getInt ("imageId"); ImageView imageView = (ImageView) findViewById (R.id.image); imageView.setImageResource (imageId);}}
Complete sample code for MainActivity
Package com.badao.selectimage;import androidx.annotation.Nullable;import androidx.appcompat.app.AppCompatActivity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.ImageView;public class MainActivity extends AppCompatActivity {@ Override protected void onActivityResult (int requestCode, int resultCode, @ Nullable Intent data) {super.onActivityResult (requestCode, resultCode, data); if (requestCode = = 200 & & resultCode = = 200) {Bundle bundle = data.getExtras (); int imageId = bundle.getInt ("imageId") ImageView imageView = (ImageView) findViewById (R.id.image); imageView.setImageResource (imageId);} @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); Button button = (Button) findViewById (R.id.button); button.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {Intent intent = new Intent (MainActivity.this,HeadActivity.class); startActivityForResult (intent,200);}});}}
These are all the contents of the article "how to call another Activity in Android and return results". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.