Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何将数据从一个xml发送到另一个xml布局?_Android_Android Layout - Fatal编程技术网

Android 如何将数据从一个xml发送到另一个xml布局?

Android 如何将数据从一个xml发送到另一个xml布局?,android,android-layout,Android,Android Layout,1.第一个活动: 此活动有两个编辑文本和两个按钮。 我需要提供登录id和密码。在这里 按钮包含登录和取消: package com.samelayout; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.wid

1.第一个
活动
: 此
活动
有两个
编辑文本
和两个
按钮
。 我需要提供登录id和密码。在这里
按钮包含登录和取消:

package com.samelayout;
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 SameLayoutActivity extends Activity {
    /** Called when the activity is first created. */
    EditText Myusername;
    EditText Mypassword;
    Button btnlogin;
    Button btncancel;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btnlogin=(Button)findViewById(R.id.loginbutton);
        btncancel=(Button)findViewById(R.id.cancelbutton);
        Myusername=(EditText)findViewById(R.id.username);
        Mypassword=(EditText)findViewById(R.id.password);    
        btnlogin.setOnClickListener(new OnClickListener()
        {           public void onClick(View v) {
                String str1=Myusername.getText().toString();
                String str2=Mypassword.getText().toString();
                Intent int1=new Intent(getApplicationContext(),Login.class);
                Bundle b= new Bundle();
                b.putString("userdata", str1);
                b.putString("userdata1", str2);
                int1.putExtras(b);
                int1.putExtras(b);
                //(int1);
                startActivityForResult(int1, 0);
            }
        });
    btncancel.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // TODO Auto-generated method stub
            finish();
        }
    });  
    }
}
2.第二次
活动
: 此
活动
从第一个
活动
检索数据。我需要向第二个xml文件显示用户详细信息

package com.samelayout;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Login extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        //Intent int1= getIntent();
        Bundle b=this.getIntent().getExtras();
        if(b!=null)
        {
            String str1=b.getString("userdata");
            String str2=b.getString("userdata1");
            TextView tv1=(TextView)findViewById(R.id.text1);
            TextView tv2=(TextView)findViewById(R.id.text2);
            tv1.setText(str1);
            tv2.setText(str2);
            setContentView(tv1);
            setContentView(tv2); }
    } }
3.
main.xml
此处:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/Linearlayout"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="1"
    android:background="@android:color/darker_gray" >

        <TextView android:layout_width="fill_parent" 
        android:text="Employee ID:" 
        android:layout_height="wrap_content" 
        android:textColor="#0000FF" 
        android:textSize="20sp"/>

        <EditText android:hint="enter ur ID" 
        android:singleLine="true"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:id="@+id/username"/>

        <TextView android:layout_width="fill_parent" 
        android:text="Password:" 
        android:layout_height="wrap_content" 
        android:textColor="#0000FF" 
        android:textSize="20dip"/>

        <EditText android:singleLine="true" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:id="@+id/password" 
        android:password="true"/>

        <Button 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="Submit" 
        android:id="@+id/loginbutton"/>

        <Button 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="Cancel" 
        android:id="@+id/cancelbutton"/>
      </LinearLayout>
6.android清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.samelayout"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name">
        <activity
            android:label="@string/app_name"
            android:name=".SameLayoutActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

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

            android:name=".Login" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

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

    </application>

</manifest>

SameLayoutActivity中替换此

 btnlogin.setOnClickListener(new OnClickListener()
        {           public void onClick(View v) {
                String str1=Myusername.getText().toString();
                String str2=Mypassword.getText().toString();
                Intent int1=new Intent(SameLayoutActivity.this,Login.class);
                Bundle b= new Bundle();
                b.putString("userdata", str1);
                b.putString("userdata1", str2);
                int1.putExtras(b);
                //(int1);
                startActivityForResult(int1, 0);
            }
        });
在您的登录活动中,替换此代码,然后重试,并告诉我发生了什么

  TextView tv1=(TextView)findViewById(R.id.text1);
  TextView tv2=(TextView)findViewById(R.id.text2);
if(b!=null)
        {
            String str1=b.getString("userdata");
            String str2=b.getString("userdata1");

            tv1.setText(str1);
            tv2.setText(str2);
            }
    }
清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.samelayout"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name">
        <activity
            android:label="@string/app_name"
            android:name=".SameLayoutActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

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

            android:name=".Login" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

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

    </application>

</manifest>



您需要通过用于启动第二个活动的
Intent
传递数据。查看此信息。

此外,将问题更改为对其他用户可能产生误解。将用户名详细信息放入SharedReferences,并从任何活动访问。
  TextView tv1=(TextView)findViewById(R.id.text1);
  TextView tv2=(TextView)findViewById(R.id.text2);
if(b!=null)
        {
            String str1=b.getString("userdata");
            String str2=b.getString("userdata1");

            tv1.setText(str1);
            tv2.setText(str2);
            }
    }
    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name">
        <activity
            android:label="@string/app_name"
            android:name=".SameLayoutActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

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

</manifest>