gpt4 book ai didi

android - 如何将文本从一个 Activity 发送到另一个 Activity ?

转载 作者:行者123 更新时间:2023-12-02 21:07:07 27 4
gpt4 key购买 nike

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(工作正常)

我想要的:我想在calculated.xml中显示文本,这是在main.class中创建的字符串,并且我希望该字符串是在名为“editText1”的文本字段中填充的文本

最佳答案

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"));
}

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:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

关于android - 如何将文本从一个 Activity 发送到另一个 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15052019/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com