填写其他活动的textview

时间:2016-05-15 10:10:54

标签: java android

我试图创建一个时间表,为此我用两个java代码创建了两个活动 这是第一个活动的xml部分的开始

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.clmentine.emploidutemps.MainActivity">


<TableLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentEnd="false"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="35dp">

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:minWidth="50dp"
        android:minHeight="40dp">

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:measureWithLargestChild="false"
            android:showDividers="middle|beginning|end"
            android:minWidth="50dp">

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:id="@+id/textView66"
                android:singleLine="true" />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:measureWithLargestChild="false"
            android:showDividers="middle|beginning|end"
            android:minWidth="55dp" >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/lundi"
                android:id="@+id/textView"
                android:maxWidth="60dp"
                android:textStyle="bold"
                android:textColor="#b829d8"
                 />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:measureWithLargestChild="false"
            android:showDividers="middle|beginning|end"
            android:minWidth="55dp" >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/mardi"
                android:id="@+id/textView2"
                android:maxWidth="60dp"
                android:textStyle="bold"
                android:textColor="#b829d8"
                />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:measureWithLargestChild="false"
            android:showDividers="middle|beginning|end"
            android:minWidth="55dp" >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/mercredi"
                android:id="@+id/textView3"
                android:maxWidth="60dp"
                android:textStyle="bold"
                android:textColor="#b829d8"
                 />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:measureWithLargestChild="false"
            android:showDividers="middle|beginning|end"
            android:minWidth="55dp" >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/jeudi"
                android:id="@+id/textView4"
                android:maxWidth="60dp"
                android:textStyle="bold"
                android:textColor="#b829d8"
                 />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:measureWithLargestChild="false"
            android:showDividers="middle|beginning|end"
            android:minWidth="55dp" >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/vendredi"
                android:id="@+id/textView5"
                android:maxWidth="60dp"
                android:textStyle="bold"
                android:textColor="#b829d8"
                 />
        </LinearLayout>

    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:minWidth="50dp"
        android:minHeight="40dp" >

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:measureWithLargestChild="false"
            android:showDividers="middle|beginning|end"
            android:minWidth="50dp" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/un"
                android:id="@+id/textView11"
                android:maxWidth="50dp"
                android:textSize="30sp"
                android:textColor="#000000"
                android:textStyle="bold" />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:measureWithLargestChild="false"
            android:showDividers="middle|beginning|end"
            android:minWidth="55dp" >

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="Small Text"
                android:id="@+id/Lundi1"
                android:maxWidth="60dp" />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:measureWithLargestChild="false"
            android:showDividers="middle|beginning|end"
            android:minWidth="60dp" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="Small Text"
                android:id="@+id/Mardi1"
                android:maxWidth="60dp" />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:measureWithLargestChild="false"
            android:showDividers="middle|beginning|end"
            android:minWidth="60dp" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="Small Text"
                android:id="@+id/Mercredi1"
                android:maxWidth="60dp" />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:measureWithLargestChild="false"
            android:showDividers="middle|beginning|end"
            android:minWidth="60dp" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="Small Text"
                android:id="@+id/Jeudi1"
                android:maxWidth="60dp" />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:measureWithLargestChild="false"
            android:showDividers="middle|beginning|end"
            android:minWidth="60dp" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="Small Text"
                android:id="@+id/Vendredi1"
                android:maxWidth="60dp" />
        </LinearLayout>
    </TableRow>

   </TableLayout>

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="40dp"
    android:text="@string/plus"
    android:id="@+id/button"
    android:maxWidth="50dp"
    android:textColor="#060606"
    android:textSize="25sp"
    android:maxHeight="40dp"
    android:textStyle="bold"
    android:layout_marginLeft="71dp"
    android:layout_marginStart="71dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"

    android:gravity="center"

    android:background="#b326ef"
    android:width="40dp"
    android:height="40dp" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="40dp"
    android:text="@string/retour"
    android:id="@+id/button2"
    android:maxWidth="100dp"
    android:textColor="#060606"
    android:textSize="20sp"
    android:maxHeight="60dp"
    android:textStyle="bold"

    android:gravity="center"

    android:background="#b326ef"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_marginRight="75dp"
    android:layout_marginEnd="75dp" />

然后java代码我能够创建一个链接活动的按钮

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

Button button;
public TextView textViewObj;

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

    button=(Button) findViewById(R.id.button);
    textViewObj=(TextView) findViewById(R.id.button);

    button.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            Intent appel = new Intent(MainActivity.this, BisActivity.class);
            startActivityForResult(appel , 1) ;
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RESULT_OK) {
        textViewObj.setText(data.getStringExtra("text")) ;
    }
}

}

然后在我的第二次活动中我尝试了

import android.app.Activity;
import android.content.Intent;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;


public class BisActivity extends Activity {

Button boutonvalider;
EditText editText ;

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

    boutonvalider=(Button) findViewById(R.id.boutonvalider);
    editText=(EditText) findViewById(R.id.edittext);

    boutonvalider.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            String textViewString = editText.getText().toString() ;
            Intent intent = getIntent();
            intent.putExtra("text", textViewString);
            setResult(RESULT_OK, intent);
            finish();

        }

    });
}
}

我想在第二个活动中在edittext中写一些东西而不是添加&#34; a&#34;添加我写的内容

请帮帮我

这是我第二个活动的xml代码

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.clmentine.emploidutemps.BisActivity">


<TextView
    android:id="@+id/matiere"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="@string/matiere"
    android:textAppearance="?android:attr/textAppearanceLarge"/>
<EditText
    android:id="@+id/rentrermatiere"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/matiere"
    android:inputType="text"
    android:layout_centerHorizontal="true"
    android:width="150dp" />


<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/valider"
    android:id="@+id/boutonvalider"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="400dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="@string/heureetdate"
    android:id="@+id/textView6"
    android:layout_below="@+id/rentrermatiere"
    android:layout_centerHorizontal="true"
    android:textSize="20sp"
    android:textColor="#070707" />

 </RelativeLayout>

2 个答案:

答案 0 :(得分:0)

在第一项活动中,您可以使用:

Intent appel = new Intent(MainActivity.this, BisActivity.class);
startActivityForResult(appel , REQUEST_CODE);

而不是startActivity();

并覆盖onActivityResult方法:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    String requestedString= intent.getStringExtra("text");
}

并在第二个活动上使用:

Intent result = new Intent();
result.putExtra("text", result stirng);
setResult(Activity.RESULT_OK, result);
finish();

答案 1 :(得分:0)

如果我理解正确,您希望从第二个活动中的EditText获取数据,并将它们放在第一个活动中的特定TextView中。

使用startActivityForResult而不是startActivity,如下所示:

button=(Button) findViewById(R.id.button);

button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
    Intent appel = new Intent(MainActivity.this, BisActivity.class);
    startActivityForResult(appel , 1) ;

现在,使用onActivityResult来检索结果:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RESULT_OK) {
        TextView tv = (TextView)findViewById(R.id.tv) ;
        tv.setText(data.getStringExtra("text")) ;
    }  
}

在你的第一个活动中你应该有这样的东西:

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    Button button;
    public TextView textViewObj;

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

       button=(Button) findViewById(R.id.button);
       textViewObj=(TextView) findViewById(R.id.button);

       button.setOnClickListener(new View.OnClickListener(){
           @Override
           public void onClick(View v){
               Intent appel = new Intent(MainActivity.this, BisActivity.class);
               startActivityForResult(appel , 1) ;
           }
       });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RESULT_OK) {
            textViewObj.setText(data.getStringExtra("text")) ;
        }  
    }

}

并在您的第二个活动(BisActivity)中,如果您希望按钮更新第一个活动textView并立即返回第一个活动,请执行以下操作:

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class BisActivity extends Activity {

    Button button;
    EditText editText ;

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

        button=(Button) findViewById(R.id.boutonvalider);
        editText=(EditText) findViewById(R.id.edittext);

        button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                String textViewString = editText.getText().toString() ;
                Intent intent = getIntent();
                intent.putExtra("text", textViewString);
                setResult(RESULT_OK, intent);
                finish();

            }

        });
    }
}