2013-02-24 1 views
0

calculated.java: (это должен командовать, чтобы показать расположение calculated.xml)Как отправить текст из одной активности в другую?

public class Calculated extends Activity { 


    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.calculated); 

    } 
} 

старше:

главный класс:

package com.barth.appie; 

import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.EditText; 


public class MainActivity extends Activity { 


Button button1; 
String text; 


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

public void addListenerOnButton() { 

    button1 = (Button) findViewById(R.id.buttoncalculate); 
    button1.setOnClickListener(new View.OnClickListener() { 

    public void onClick(View view) { 

     EditText editText = (EditText)findViewById(R.id.editText1); 
     String text = editText.getText().toString(); 

      Intent myIntent = new Intent(view.getContext(), Calculated.class); 
      startActivityForResult(myIntent, 0); 

     } 
    }); 
} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 



} 

calculated.xml: (это то, что он должен показать после нажатия кнопки)

<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" 
tools:context=".MainActivity" > 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="42dp" 
    android:text="@string/text" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

</RelativeLayout> 

Кроме того, У меня есть <string name="text">Value</string> на strings.xml, а в другой класс под названием calculated.class, я его так, что он выводит calculated.xml (который работает отлично)

Что я хочу: Я хочу, чтобы отобразить текст в расчетном .xml, который является строкой, производится в main.class, и я хочу, чтобы строка, текст заполняется в текстовом поле под названием «editText1»

+0

Исследование удивительной вещи под названием «Дополнительно». –

ответ

2
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.EditText; 


public class MainActivity extends Activity { 

Button button1; 
String text; 

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

public void addListenerOnButton() { 

    button1 = (Button) findViewById(R.id.buttoncalculate); 
    button1.setOnClickListener(new View.OnClickListener() { 

    public void onClick(View view) { 

     EditText editText = (EditText)findViewById(R.id.editText1); 
     String text = editText.getText().toString(); 

      Intent myIntent = new Intent(view.getContext(),Calculated.class); 
      myIntent.putExtra("mytext",text); 
      startActivity(myIntent); 

     } 
    }); 
} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
    } 
} 

Calculated.java

public class Calculated extends Activity { 

TextView mTextview; 

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

     mTextview = (TextView)findViewById(R.id.textView1); 

     mTextview.setText(getIntent().getStringExtra("mytext")); 
} 

.xml

<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" 
tools:context=".MainActivity" > 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="42dp" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

</RelativeLayout> 
+0

Но как преобразовать текстовый ввод в строку? – John

+0

Я сейчас обновил .. – moDev

+0

Спасибо, я добавил это в свой класс. Но когда я набираю @ string/myEditValue, он говорит, что не существует ....как мне выводить строку в layout.xml ... – John

1

чтобы отправить данные с одного вида деятельности на другой вид деятельности

MainAc tivity.java - это первое действие, из которого вы хотите отправить данные в другое действие.

Intent myIntent = new Intent(view.getContext(), Calculated.class); 
myIntent.putExtra("text", text); 
startActivity(myIntent); 

Calculated.java является второй активностью, получать данные о намерении, что вы перейти от MainActivity.java

String text = myIntent.getStringExtra("text"); 
TextView textView = (TextView)findViewById(R.id.textView1); 
textView.setText(text); 
+0

@Bart Теперь я публикую текст edittext, пожалуйста, проверьте его и примите, если он решит вашу проблему. –

+0

Спасибо, я добавил это в свой класс. Но когда я набираю @ string/text, он говорит, что не существует .... как я могу вывести строку в layout.xml ... – John

+0

Я обновил ответ, проверьте, но где вы используете, как это В edittext вид в макете вам не следует удалять строку android: text = "@ string/text". –