Jump and transmit data instances within a single app Android

To achieve the effect of the picture: to achieve the main page Button1 and Button2 click; Button1 click to enter the interface shows a section of text; Button2 click into the screen there is a text box, and click OK after editing can jump back to the main interface and the bottom of the main interface text editing prior to display.

Specific steps are as follows:

1. To establish a method according to the project prior to the establishment of a project (see " Android the Application simple example (generates" the Hello World ! ") " A text), and name it the Hello World , layout called main , Activity named HelloWorldActivity .

2. Once you have created no mistake, paste main file, then copy the two were named as activityb , actiivtyc ; and paste HelloWorldActivity file, copy the two named ActvityB , ActivityC . Modify java file in the onCreate () .

3. After all of our Activity is required in Androdmanifest.xml register file, namely:

<actiivty android:name=.ActivityB />

<actiivty android:name=.ActivityB /

4. After editing three layout page file, the code visible, are taken in the case of linear layout, while giving each control plus the above mentioned id , position size layout, text rendering. That is a schematic view of Target required to achieve several results above way.

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    
    <Button android:id="@+id/button1"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:text="Button1" />  
        
    <Button android:id="@+id/button2"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:text="Button2" />
    
    <TextView android:id="@+id/tvDisplay"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />  
</LinearLayout> 
activityb.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    
    <TextView android:id="@+id/tvActivityb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is ActivityB, Welcome!" />  
</LinearLayout> 

activityc.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    
    <EditText android:id="@+id/etActivityc"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" /> 
            
    <Button android:id="@+id/buttonc1"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:text="确定" />   
</LinearLayout> 

5. Edit main page when opening java file, which first set up the layout file ->

After obtaining control, the control variable is obtained, just as in js objects to get control of the same ->

After listening to bind a control event, a property of this control is to listen, that this attribute parameters, we must describe what the listener, how to listen, need new object as a parameter to this method include a click ->

Click after use intent can achieve the jump page is divided into two, one to describe the jump, one for the start intent , (pay attention here and start start callback can not callback).

package com.HoD.helloworld;

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 HelloWorldActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//将布局文件设置为main.xml
		setContentView(R.layout.main);
		//得到两个Button控件
		Button mButton1 = (Button)findViewById(R.id.button1);
		Button mButton2 = (Button)findViewById(R.id.button2);
		//为Button1绑定单击事件
		mButton1.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v){
				// TODO Auto-generated method stub
				//使用intent启动ActivityB
				Intent _intent = new Intent(HelloWorldActivity.this, ActivityB.class);
				startActivity(_intent);		
			}
		});
		//为Button2绑定单击事件
		mButton2.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v){
				// TODO Auto-generated method stub
				//使用intent启动ActivityB
				Intent _intent = new Intent(HelloWorldActivity.this, ActivityC.class);
				startActivityForResult(_intent, 100);		
			}
		});	
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {	
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode == 100 && resultCode == Activity.RESULT_OK){
			String val = data.getExtras().getString("helloworld");
			TextView textView = (TextView)findViewById(R.id.tvDisplay);
			textView.setText("来自ActivityC的值:"+ val);
		}
	}
}

6. Edit ActivityC of java files, is also first set the layout file ->

After the acquisition is also a button instance, and set to listen for events ->

Different methods that monitor different objects taken first instantiate the Intent () Object ->

After obtaining the formula bar example, and then get its value ->

Using the value putExtra () is stored to the Intent () to instantiate object ->

This value is returned, and close the page.

package com.HoD.helloworld;

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.EditText;

public class ActivityC extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//设置activityc.xml为布局文件
		setContentView(R.layout.activityc);
		//得到Button实例
		Button button1 = (Button)findViewById(R.id.buttonc1);
		button1.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v){
				// TODO Auto-generated method stub
				//实例化一个intent对象
				Intent data = new Intent();
				//获取EditText实例
				EditText editText = (EditText)findViewById(R.id.etActivityc);
				//得到EditText的值
				String val = editText.getText().toString();
				//将EditText的值存到intent对象中(以键值的形式)
				data.putExtra("helloworld", val);
				//调用setResult方法,将intent对象(data)传回父Activity
				setResult(Activity.RESULT_OK, data);
				//关闭当前Activity
				finish();
			}
		});
	}
}

7. In the HelloWorld the java file, get the value back pass, not present Activity lifecycle onCreate () , and in the onActivityResult () , it is determined after the value of legitimate back pass, takes a value into a variable, and examples of the main in the hidden text box, the value shown (in the above has been shown to result HelloWorldActivity .java in) .

8. which are typical problems encountered, the Intent CAN BE resolved not to A of the type , or the listener does not recognize the internal onClick (View v) does not recognize.

These results are the appropriate package is not imported, because our previous JDK good setting, JAR package is not intentionally added, it is generally the appropriate package import on it (in general is on the top of the mouse will automatically be prompted to import missing package). After the error if it is, then Clean look like, this is the eclipse is not automatically compile the source code to the Build / classes (or other classes directory) causes a type not found.

import android.content.DialogInterface.OnClickListener; or, if introduced into the sentence, will not, packages should be the import android.view.View.OnClickListener;








Guess you like

Origin blog.csdn.net/HoD_DoH/article/details/53605894