在不同的活动中在EditText和Text View之间传输数据

时间:2013-05-10 06:01:57

标签: android

我是Android应用开发的新手。我有一个问题。 在第一个活动中,我创建了10行。每行包含一个链接到第二个活动的下一个按钮。 在第二个活动中,我有edittext字段输入用户详细信息,如帐户名,密码等。每次我更新我的帐户名称,当我按下android后退按钮时,该行应包含更新的名称。但我无法将帐户名称传递给第一个活动。

以下是我的第一项活动代码:

public class AccountSetup extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.account_main);
        this.initViews();
    }

    private void initViews(){ 
        TextView user1  = (TextView)findViewById(R.id.user1);
        Button iconNext1  = (Button)findViewById(R.id.iconNext1);  
        iconNext1.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
                Intent Intent1 = new Intent(AccountSetup.this, AccountSettingActivity1.class);
                Intent1.putExtra("rowid","1"); 
                startActivityForResult(Intent1, 100);
            }
        });

        Button iconNext2  = (Button)findViewById(R.id.iconNext2);       
        iconNext2.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
                Intent Intent2 = new Intent(AccountSetup.this, AccountSettingActivity2.class);
                startActivity(Intent2);
                finish();
            }
        });

        Button iconNext3  = (Button)findViewById(R.id.iconNext3);       
        iconNext3.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
                Intent Intent3 = new Intent(AccountSetup.this, AccountSettingActivity3.class);
                //onNewIntent((Intent) v.getTag());
                Intent3.putExtra("rowid","3");
                startActivity(Intent3);
                finish();
            }
        });

        Button iconNext4  = (Button)findViewById(R.id.iconNext4);       
        iconNext4.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
                Intent Intent4 = new Intent(AccountSetup.this, AccountSettingActivity4.class);
                //onNewIntent((Intent) v.getTag());
                Intent4.putExtra("rowid","4");
                startActivity(Intent4);
                finish();
            }
        });

        Button iconNext5  = (Button)findViewById(R.id.iconNext5);       
        iconNext5.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
                Intent Intent5 = new Intent(AccountSetup.this, AccountSettingActivity5.class);
                //onNewIntent((Intent) v.getTag());
                Intent5.putExtra("rowid","5");
                startActivity(Intent5);
                finish();
            }
        });
        Button iconNext6  = (Button)findViewById(R.id.iconNext6);       
        iconNext6.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
                Intent Intent6 = new Intent(AccountSetup.this, AccountSettingActivity6.class);
                //onNewIntent((Intent) v.getTag());
                Intent6.putExtra("rowid","6");
                startActivity(Intent6);
                finish();
            }
        });
        Button iconNext7  = (Button)findViewById(R.id.iconNext7);       
        iconNext7.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
                Intent Intent7 = new Intent(AccountSetup.this, AccountSettingActivity7.class);
                //onNewIntent((Intent) v.getTag());
                Intent7.putExtra("rowid","7");
                startActivity(Intent7);
                finish();
            }
        });

        Button iconNext8  = (Button)findViewById(R.id.iconNext8);       
        iconNext8.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
                Intent Intent8 = new Intent(AccountSetup.this, AccountSettingActivity8.class);
                //onNewIntent((Intent) v.getTag());
                Intent8.putExtra("rowid","8");
                startActivity(Intent8);
                finish();
            }
        });
        Button iconNext9  = (Button)findViewById(R.id.iconNext9);       
        iconNext9.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
                Intent Intent9 = new Intent(AccountSetup.this, AccountSettingActivity9.class);
                //onNewIntent((Intent) v.getTag());
                Intent9.putExtra("rowid","9");
                startActivity(Intent9);
                finish();
            }
        });
        Button iconNext10  = (Button)findViewById(R.id.iconNext10);       
        iconNext10.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
                Intent Intent10 = new Intent(AccountSetup.this, AccountSettingActivity10.class);
                //onNewIntent((Intent) v.getTag());
                Intent10.putExtra("rowid","10");
                startActivity(Intent10);
                finish();
            }
        });
    }

    @Override
    public void onBackPressed() {
        Intent i = new Intent(AccountSetup.this, WelcomeActivity.class);
        startActivity(i);
        finish();
        super.onBackPressed();
    }

    @Override
    protected void onActivityResult(int requestCode,int resultCode, Intent data) {
       if (requestCode == 100) {
           if (resultCode == RESULT_OK) {      
               String accountName1 = data.getStringExtra("accountName1");          
           }
       }
    }
}

以下是第二项活动的代码。

public class AccountSettingActivity1 extends Activity{

    private EditText etAccountName;
    private EditText etWanIp;
    private EditText etLocalIp;
    private EditText etPort;
    private EditText etPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.account_tab_content_setting);
        this.initViews();

    }
        private void initViews(){
            etAccountName = (EditText)this.findViewById(R.id.etAccountName);
            etWanIp = (EditText)this.findViewById(R.id.etWanIp);
            etLocalIp = (EditText)this.findViewById(R.id.etLocalIp);
            etPort = (EditText)this.findViewById(R.id.etPort);
            etPassword = (EditText)this.findViewById(R.id.etPassword);

            // Assigns value
            SharedPreferences  sp = PreferenceManager.getDefaultSharedPreferences(this);
            etAccountName.setText(sp.getString("accountName1", ""));
            etWanIp.setText(sp.getString("wanIp1", ""));
            etLocalIp.setText(sp.getString("localIp1", ""));
            etPort.setText(sp.getString("port1", ""));
            etPassword.setText(sp.getString("password1", ""));

            etWanIp.setOnFocusChangeListener(new OnFocusChangeListener(){
                @Override
                public void onFocusChange(View arg0, boolean hasFocus) {
                    if(!hasFocus){
                        System.out.println("lost focus");
                        AccountSettingActivity1.this.saveSettings();
                    }
                }
            });
        }

        private void saveSettings(){
            String accountName1 = etAccountName.getText().toString();
            String wanIp1 = etWanIp.getText().toString();
            String localIp1 = etLocalIp.getText().toString();
            String port1 = etPort.getText().toString();
            String password1 = etPassword.getText().toString();

            accountName1 = (accountName1.trim().length() == 0)? "User": accountName1;
            wanIp1 = (wanIp1.trim().length() == 0)? "0.0.0.0": wanIp1;
            localIp1 = (localIp1.trim().length() == 0)? "0.0.0.0": localIp1;
            port1 = (port1.trim().length() == 0)? "8000": port1;
            password1 = (password1.trim().length() == 0)? "xxxx": password1;

            etAccountName.setText(accountName1);
            etWanIp.setText(wanIp1);
            etLocalIp.setText(localIp1);
            etPort.setText(port1);
            etPassword.setText(password1);

            SharedPreferences.Editor editor = PreferenceManager
                    .getDefaultSharedPreferences(this).edit();
            editor.putString("accountName1", etAccountName.getText().toString());
            editor.putString("wanIp1", etWanIp.getText().toString());
            editor.putString("localIp1", etLocalIp.getText().toString());
            editor.putString("port1", etPort.getText().toString());
            editor.putString("password1", etPassword.getText().toString());
            editor.commit();
        }

/*  @Override
    public void onBackPressed() {
        saveSettings();
        Intent i = new Intent(AccountSettingActivity1.this, AccountSetup.class);
        startActivity(i);
        finish();
        super.onBackPressed();
    }*/

        public void onBackPressed() {

             saveSettings();
             //final EditText Eclass1;

            EditText et = (EditText)findViewById(R.id.etAccountName);
            String s= et.getText().toString();
            Intent i = new Intent(AccountSettingActivity1.this, AccountSetup.class);
            i.putExtra("accountName1" ,s);
            setResult(RESULT_OK,  i);     
            finish();
            super.onBackPressed();
        }   

    @Override
    protected void onPause() {
        // When user leaves this tab, saves the values
        this.saveSettings();
        super.onPause();
    }
}

第一个活动xml代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:AccountSetup="http://schemas.android.com/apk/res/com.example.play"
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

<!--     Account Toolbar -->
    <RelativeLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#324F85" >
        <Button
            android:layout_width="54dp"
            android:layout_height="30dp"
            android:layout_alignParentLeft="true"
            android:background="@drawable/ic_btn_done"
            android:layout_centerVertical="true"/>
        <TextView 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:text="@string/selectAccount" 
            android:textAppearance="?android:attr/textAppearanceMedium" 
            android:textSize="15dp"
            android:layout_centerHorizontal="true" 
            android:layout_centerVertical="true"
            android:gravity="center"
            android:textStyle="bold" />
        <Button
            android:id="@+id/btnAdd" 
            android:layout_width="54dp"
            android:layout_height="30dp"
            android:layout_alignParentRight="true"
            android:background="@drawable/ic_btn_add_account"
            android:layout_centerVertical="true" />

        <ImageView
            android:layout_width="4dp"
            android:layout_height="30dp"
            android:layout_alignTop="@+id/btnDone"
            android:layout_toRightOf="@+id/btnDone"
            android:src="@drawable/toolbar_seperator" />

        <ImageView
            android:layout_width="4dp"
            android:layout_height="30dp"
            android:layout_alignTop="@+id/btnAdd"
            android:layout_toLeftOf="@+id/btnAdd"
            android:src="@drawable/toolbar_seperator" />
    </RelativeLayout>

        <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="15dp" 
        android:paddingRight="15dp" 
        android:background="@drawable/logo_small_white" >

        <ScrollView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

            <TableLayout
                android:id="@+id/tlStatus"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:stretchColumns="1" >

                <TableRow
                    style="@style/tableRow"
                    android:id="@+id/row1"
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:layout_weight="1"
                    android:gravity="center_vertical" >

                    <TextView
                        android:id="@+id/user1"
                        style="@style/textSettingLabel"
                        android:layout_weight="1" />
                    <Button
                        android:id="@+id/iconNext1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="right"
                        android:layout_weight="1"
                        android:background="@drawable/icon_next" />
                </TableRow>

                <View style="@style/tableRowBorder" />

                <TableRow
                    style="@style/tableRow"
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:layout_weight="1"
                    android:gravity="center_vertical" >

                    <TextView
                        android:id="@+id/user2"
                        style="@style/textSettingLabel"
                        android:layout_weight="1" />
                    <Button
                        android:id="@+id/iconNext2"
                        android:layout_width="wrap_content"
                        android:layout_gravity="right"
                        android:layout_weight="1"
                        android:scaleType="fitEnd"
                        android:background="@drawable/icon_next" />
                </TableRow>

                <View style="@style/tableRowBorder" />

                <TableRow
                    style="@style/tableRow"
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:layout_weight="1"
                    android:gravity="center_vertical" >

                    <TextView
                        android:id="@+id/user3"
                        style="@style/textSettingLabel"
                        android:layout_weight="1" />
                    <Button
                        android:id="@+id/iconNext3"
                        android:layout_width="wrap_content"
                        android:layout_gravity="right"
                        android:layout_weight="1"
                        android:scaleType="fitEnd"
                        android:background="@drawable/icon_next" />
                </TableRow>

                <View style="@style/tableRowBorder" />

                <TableRow
                    style="@style/tableRow"
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:layout_weight="1"
                    android:gravity="center_vertical" >

                    <TextView
                        android:id="@+id/user4"
                        style="@style/textSettingLabel"
                        android:layout_weight="1" />
                    <Button
                        android:id="@+id/iconNext4"
                        android:layout_width="wrap_content"
                        android:layout_gravity="right"
                        android:layout_weight="1"
                        android:scaleType="fitEnd"
                        android:background="@drawable/icon_next" />
                </TableRow>

                <View style="@style/tableRowBorder" />

                <TableRow
                    style="@style/tableRow"
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:layout_weight="1"
                    android:gravity="center_vertical" >

                    <TextView
                        style="@style/textSettingLabel"
                        android:id="@+id/user5"
                        android:layout_alignBaseline="@+id/etZone_1_1"
                        android:text="User 1" />
                    <Button
                        android:id="@+id/iconNext5"
                        android:layout_width="wrap_content"
                        android:layout_gravity="right"
                        android:layout_weight="1"
                        android:scaleType="fitEnd"
                        android:background="@drawable/icon_next" />
                </TableRow>

                <View style="@style/tableRowBorder" />

                <TableRow
                    style="@style/tableRow"
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:layout_weight="1"
                    android:gravity="center_vertical" >

                    <TextView
                        style="@style/textSettingLabel"
                        android:id="@+id/user6"
                        android:layout_alignBaseline="@+id/etZone_1_1"
                        android:text="User 1" />
                    <Button
                        android:id="@+id/iconNext6"
                        android:layout_width="wrap_content"
                        android:layout_gravity="right"
                        android:layout_weight="1"
                        android:scaleType="fitEnd"
                        android:background="@drawable/icon_next" />
                </TableRow>

                <View style="@style/tableRowBorder" />

                <TableRow
                    style="@style/tableRow"
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:layout_weight="1"
                    android:gravity="center_vertical" >

                    <TextView
                        style="@style/textSettingLabel"
                        android:id="@+id/user7"
                        android:layout_alignBaseline="@+id/etZone_1_1"
                        android:text="User 1" />
                    <Button
                        android:id="@+id/iconNext7"
                        android:layout_width="wrap_content"
                        android:layout_gravity="right"
                        android:layout_weight="1"
                        android:scaleType="fitEnd"
                        android:background="@drawable/icon_next" />
                </TableRow>

                <View style="@style/tableRowBorder" />

                <TableRow
                    style="@style/tableRow"
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:layout_weight="1"
                    android:gravity="center_vertical" >

                    <TextView
                        style="@style/textSettingLabel"
                        android:id="@+id/user8"
                        android:layout_alignBaseline="@+id/etZone_1_1"
                        android:text="User 1" />
                    <Button
                        android:id="@+id/iconNext8"
                        android:layout_width="wrap_content"
                        android:layout_gravity="right"
                        android:layout_weight="1"
                        android:scaleType="fitEnd"
                        android:background="@drawable/icon_next" />
                </TableRow>

                <View style="@style/tableRowBorder" />

                <TableRow
                    style="@style/tableRow"
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:layout_weight="1"
                    android:gravity="center_vertical" >

                    <TextView
                        style="@style/textSettingLabel"
                        android:id="@+id/user9"
                        android:layout_alignBaseline="@+id/etZone_1_1"
                        android:text="User 1" />
                    <Button
                        android:id="@+id/iconNext9"
                        android:layout_width="wrap_content"
                        android:layout_gravity="right"
                        android:layout_weight="1"
                        android:scaleType="fitEnd"
                        android:background="@drawable/icon_next" />
                </TableRow>

                <View style="@style/tableRowBorder" />

                <TableRow
                    style="@style/tableRow"
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:layout_weight="1"
                    android:gravity="center_vertical" >

                    <TextView
                        style="@style/textSettingLabel"
                        android:id="@+id/user10"
                        android:layout_alignBaseline="@+id/etZone_1_1"
                        android:text="User 1" />
                    <Button
                        android:id="@+id/iconNext10"
                        android:layout_width="wrap_content"
                        android:layout_gravity="right"
                        android:layout_weight="1"
                        android:scaleType="fitEnd"
                        android:background="@drawable/icon_next" />
                </TableRow>             
                <View style="@style/tableRowBorder" />              
            </TableLayout>
        </ScrollView>
    </RelativeLayout>
</LinearLayout>

第二个活动的xml代码:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout 
       xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:ToolBar="http://schemas.android.com/apk/res/com.example.play"
       xmlns:TextViewMyRiadPro="http://schemas.android.com/apk/res/com.example.play"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:orientation="vertical">

       <ViewSwitcher
            android:id="@+id/switcher"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

            <ScrollView
                android:id="@+id/settingsView"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:background="@drawable/logo_small_white" >

                <RelativeLayout
                    android:layout_width="fill_parent"
                    android:layout_height="565dp"
                    android:padding="15dp" >

                    <TextView
                        android:id="@+id/tvAccount"
                        android:text="Name"
                        android:layout_alignBaseline="@+id/etAccountName"
                        style="@style/textSettingLabel" />

                    <EditText
                        android:id="@+id/etAccountName"
                        style="@style/textSettingEdit"
                        android:layout_alignLeft="@+id/etWanIp"
                        android:inputType="text" />

                    <TextView
                        android:id="@+id/tvAccount"
                        android:layout_below="@+id/etAccountName"
                        android:text="User Account Name"
                        style="@style/textSettingHint"  />

                    <TextView
                        android:id="@+id/tvWan"
                        android:text="Wan IP"
                        android:layout_alignBaseline="@+id/etWanIp"
                        style="@style/textSettingLabel" />
                    <EditText
                        android:id="@+id/etWanIp"
                        android:layout_below="@+id/tvAccount"
                        android:layout_toRightOf="@+id/tvWan"
                        style="@style/textSettingEdit" android:inputType="text"/>
                    <TextView
                        android:id="@+id/tvWanHint"
                        android:layout_below="@+id/etWanIp"
                        android:text="Port forwarding is required in order to establish connection from the internet"
                        style="@style/textSettingHint"  />      

                    <TextView
                        android:id="@+id/tvLocal"
                        android:text="Local IP"
                        android:layout_alignBaseline="@+id/etLocalIp"
                        style="@style/textSettingLabel" />
                    <EditText
                        android:id="@+id/etLocalIp"
                        android:layout_below="@+id/tvWanHint"
                        android:layout_toRightOf="@+id/tvLocal"
                        style="@style/textSettingEdit" android:inputType="text"/>   
                    <TextView
                        android:id="@+id/tvLocalHint"
                        android:layout_below="@+id/etLocalIp"
                        android:text="The IP address of the alarm system in the local area network. 192.168.1.234 is the default address."
                        style="@style/textSettingHint" />   

                    <TextView
                        android:id="@+id/tvPort"
                        android:text="Port"
                        android:layout_alignBaseline="@+id/etPort"
                        style="@style/textSettingLabel" />
                    <EditText
                        android:id="@+id/etPort"
                        android:layout_below="@+id/tvLocalHint"
                        android:layout_toRightOf="@+id/tvPort"
                        android:inputType="number"
                        style="@style/textSettingEdit" />   
                    <TextView
                        android:id="@+id/tvPortHint"
                        android:layout_below="@+id/etPort"
                        android:text="The connection port of the alarm system. 8000 is the default port."
                        style="@style/textSettingHint" />

                    <TextView
                        android:id="@+id/tvPassword"
                        android:text="Password"
                        android:layout_alignBaseline="@+id/etPassword"
                        style="@style/textSettingLabel" />
                    <EditText
                        android:id="@+id/etPassword"
                        android:layout_below="@+id/tvPortHint"
                        android:layout_toRightOf="@+id/tvPassword"
                        style="@style/textSettingEdit" android:inputType="textPassword"/>   
                    <TextView
                        android:layout_below="@+id/etPassword"
                        android:id="@+id/tvPasswordHint"
                        android:text="Your 4 digits password to access the alarm system."
                        style="@style/textSettingHint" />
                </RelativeLayout>
            </ScrollView>           
       </ViewSwitcher>
</LinearLayout>

2 个答案:

答案 0 :(得分:1)

使用startActivityForResult()开始第二个活动

startActivityForResult(intent, requestcode)

当第二个活动完成后,执行以下操作,意图可以保存您需要传递回activity1的值

setResult(RESULT_OK, intent)
finish();

现在在Activity1上,覆盖onActivityResult()

onActivityResult()
{
  //update your textview here.
}

答案 1 :(得分:0)

deepdroid的回答是正确的,但你有另外一种选择

设置public static String value=null;因此,它不依赖于整个活动。