Java 如何在Android中将用户输入从一个活动传递到另一个活动
我正在创建一个基本的转换应用程序,它可以转换用户输入的公里结数,反之亦然。目前,它显示结果的位置与用户最初输入图形的位置相同。我想知道如何在新活动中显示结果?目前,正如我所说,我只是设法让它出现在用户最初输入他们的图形的同一位置,然后第二个活动弹出。我错过了什么?请查看我的代码,谢谢: MainActivity.javaJava 如何在Android中将用户输入从一个活动传递到另一个活动,java,android,android-intent,android-activity,Java,Android,Android Intent,Android Activity,我正在创建一个基本的转换应用程序,它可以转换用户输入的公里结数,反之亦然。目前,它显示结果的位置与用户最初输入图形的位置相同。我想知道如何在新活动中显示结果?目前,正如我所说,我只是设法让它出现在用户最初输入他们的图形的同一位置,然后第二个活动弹出。我错过了什么?请查看我的代码,谢谢: MainActivity.java package winfield.joe.wind.v1; import android.os.Bundle; import android.app.Activity; im
package winfield.joe.wind.v1;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
public class MainActivity extends Activity {
// public var
private EditText text;
// default func
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Toast.makeText(this, "onCreate!", Toast.LENGTH_LONG).show();
setContentView(R.layout.activity_main);
// findViewById = Finds a view that was identified by the id attribute
// from the XML that was processed in onCreate(Bundle).
// (EditText) = typecast
text = (EditText) findViewById(R.id.userInput);
}
//Will be executed by clicking on the calculate button because we assigned "calc" to the "onClick" Property
public void calc(View view) {
RadioButton toKilometers = (RadioButton) findViewById(R.id.toKilometers);
RadioButton toKnots = (RadioButton) findViewById(R.id.toKnots);
if (text.getText().length() == 0) {
// if the text field is empty show the message "enter a valid number" via toast message
Toast.makeText(this, "enter a valid number", Toast.LENGTH_LONG).show();
} else {
int result = R.string.userInput;
Intent i = new Intent(MainActivity.this, SecondActivity.class);
putExtra("userInput", result);
startActivity(i);
// parse input Value from Text Field
double inputValue = Double.parseDouble(text.getText().toString());
// convert to...
if (toKilometers.isChecked()) {
text.setText(String.valueOf(convertToKM(inputValue)));
// uncheck "to km" Button
toKilometers.setChecked(false);
// check "to knots" Button
toKnots.setChecked(true);
} else { /* if toKnots button isChecked() */
text.setText(String.valueOf(convertToKnots(inputValue)));
// uncheck "to knots" Button
toKnots.setChecked(false);
// check "to km" Button
toKilometers.setChecked(true);
}
}
}
private void putExtra(String string, int result) {
// TODO Auto-generated method stub
}
private double convertToKM(double inputValue) {
// convert knots to km
return (inputValue * 1.8);
}
private double convertToKnots(double inputValue) {
// convert km to knots
return (inputValue * 0.539956803);
}
}
package winfield.joe.wind.v1;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class SecondActivity extends Activity {
Bundle extras = getIntent().getExtras();
int result = extras.getInt("results");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
//onClick GoBack method assigned to the Go Back? button which returns the user to main activity from the second activity
public void GoBack(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Are you sure?");
// set dialog message
builder .setCancelable(false)
.setPositiveButton("Convert Again",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
//You return to Main Activity
Intent intent = new Intent(SecondActivity.this, MainActivity.class);
startActivity(intent);
}
})
.setNeutralButton("Back to Home",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
//You return to the home page
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
}
})
.setNegativeButton("View Results",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close the dialog box and do nothing
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
SecondActivity.java
package winfield.joe.wind.v1;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
public class MainActivity extends Activity {
// public var
private EditText text;
// default func
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Toast.makeText(this, "onCreate!", Toast.LENGTH_LONG).show();
setContentView(R.layout.activity_main);
// findViewById = Finds a view that was identified by the id attribute
// from the XML that was processed in onCreate(Bundle).
// (EditText) = typecast
text = (EditText) findViewById(R.id.userInput);
}
//Will be executed by clicking on the calculate button because we assigned "calc" to the "onClick" Property
public void calc(View view) {
RadioButton toKilometers = (RadioButton) findViewById(R.id.toKilometers);
RadioButton toKnots = (RadioButton) findViewById(R.id.toKnots);
if (text.getText().length() == 0) {
// if the text field is empty show the message "enter a valid number" via toast message
Toast.makeText(this, "enter a valid number", Toast.LENGTH_LONG).show();
} else {
int result = R.string.userInput;
Intent i = new Intent(MainActivity.this, SecondActivity.class);
putExtra("userInput", result);
startActivity(i);
// parse input Value from Text Field
double inputValue = Double.parseDouble(text.getText().toString());
// convert to...
if (toKilometers.isChecked()) {
text.setText(String.valueOf(convertToKM(inputValue)));
// uncheck "to km" Button
toKilometers.setChecked(false);
// check "to knots" Button
toKnots.setChecked(true);
} else { /* if toKnots button isChecked() */
text.setText(String.valueOf(convertToKnots(inputValue)));
// uncheck "to knots" Button
toKnots.setChecked(false);
// check "to km" Button
toKilometers.setChecked(true);
}
}
}
private void putExtra(String string, int result) {
// TODO Auto-generated method stub
}
private double convertToKM(double inputValue) {
// convert knots to km
return (inputValue * 1.8);
}
private double convertToKnots(double inputValue) {
// convert km to knots
return (inputValue * 0.539956803);
}
}
package winfield.joe.wind.v1;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class SecondActivity extends Activity {
Bundle extras = getIntent().getExtras();
int result = extras.getInt("results");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
//onClick GoBack method assigned to the Go Back? button which returns the user to main activity from the second activity
public void GoBack(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Are you sure?");
// set dialog message
builder .setCancelable(false)
.setPositiveButton("Convert Again",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
//You return to Main Activity
Intent intent = new Intent(SecondActivity.this, MainActivity.class);
startActivity(intent);
}
})
.setNeutralButton("Back to Home",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
//You return to the home page
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
}
})
.setNegativeButton("View Results",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close the dialog box and do nothing
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
活动\u main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:background="@color/bgColor"
android:orientation="vertical">
<!--TITLE-->
<TextView
android:id="@+id/titleMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="22dp"
android:gravity="center"
android:text="@string/titleMain"
android:textColor="@color/textColor"
android:textColorHint="@color/textColor"
android:textColorLink="#ffffff"
android:textSize="30sp" />
<!--USER-INPUT-->
<EditText
android:id="@+id/userInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:ems="10"
android:hint="@+string/userInput"
android:inputType="numberDecimal"
android:paddingEnd="40dp"
android:paddingStart="20dp"
android:paddingTop="30dp" >
<requestFocus />
</EditText>
<!--TWO RADIO BUTTONS-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<!--toKNOTS-->
<RadioButton
android:id="@+id/toKnots"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_weight="0.04"
android:checked="true"
android:text="@string/toKnots" />
<!--toKM-->
<RadioButton
android:id="@+id/toKilometers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginEnd="20dp"
android:layout_marginTop="10dp"
android:text="@string/toKilometers" />
</LinearLayout>
<!--CALCULATE-->
<Button
android:id="@+id/valueCalc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginEnd="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="30dp"
android:onClick="calc"
android:text="@string/valueCalc" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:background="@color/bgColor"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
<!-- TITLE -->
<TextView
android:id="@+id/titleResults"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="24dp"
android:gravity="center"
android:text="@string/titleResults"
android:textColor="@color/textColor"
android:textColorHint="@color/textColor"
android:textColorLink="#ffffff"
android:textSize="25sp" />
<EditText
android:id="@+id/results"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="20dp"
android:layout_marginTop="30dp"
android:ems="10"
android:hint="@string/results"
android:inputType="numberDecimal"
android:textColorHint="@color/textColor" >
<requestFocus />
</EditText>
<Button
android:id="@+id/GoBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="40dp"
android:text="@string/GoBack"
android:onClick="GoBack" />
</LinearLayout>
如果您想将任何内容从一个活动传递到另一个活动,可以使用
putExtra
进行传递,它会将数据从一个活动发送到另一个活动,并在另一个活动中使用getExtras
获取该数据
要发送数据
String str = "value";
Intent launchResult = new Intent(MainActivity.this, SecondActivity.class);
putExtra("key", str);
startActivity(launchResult);
要在另一个活动中接收数据
Bundle extras = getIntent().getExtras();
String str = extras.getStr("key");
编辑:
在我的回答中,键
是在发送方活动
和接收方活动
中必须保持相同的任何唯一值
之所以出现错误,是因为您传递了一个字符串值,其中必须传递键,并且接收器中的键活动也不匹配
请参见下面的更改,它应该可以工作
MainActivity.class
int result = R.string.userInput;
Intent i = new Intent(MainActivity.this, SecondActivity.class);
putExtra("userInput", result);
startActivity(i);
Bundle extras = getIntent().getExtras();
int result = extras.getInt("userInput");
SecondActivity.class
int result = R.string.userInput;
Intent i = new Intent(MainActivity.this, SecondActivity.class);
putExtra("userInput", result);
startActivity(i);
Bundle extras = getIntent().getExtras();
int result = extras.getInt("userInput");
使用下面的代码将值从一个活动传递到另一个活动
Intent launchResult = new Intent(MainActivity.this, SecondActivity.class);
launchResult.putExtra("output",result);
您无法编辑我的答案来解释您遇到的错误,请在我的答案上写下评论,以便我能够理解您仍然存在的问题。很抱歉,单击了错误的按钮。。我在你的回答下提交了更多的进展。谢谢,J。很抱歉,请点击编辑而不是回复。在我的主要活动中,我输入了:
code
intresult=R.string.userInput;意图i=新意图(MainActivity.this,SecondActivity.class);putExtra(getString(R.string.userInput),result);星触觉(i)code
在我的第二个活动中,我输入了:code
Bundle extras=getIntent().getExtra();int result=extras.getInt(“用户输入”)<代码>代码以适合我的数据类型和id,但我仍然会遇到错误。。有什么想法吗?哦,好吧,我明白为什么了。现在只有一个错误在code
Bundle extras=getIntent().getExtra()上<代码>代码,内容为:“类型意图的getExtra()方法未定义”。不过,有大约20-30个建议的修复程序。这修复了Eclipse中的错误,但现在当我单击按钮更改活动时,它崩溃了。我不太清楚为什么,没有任何错误。我不太确定我还能给你看些什么?@joewinfield91编辑你的问题并添加你的logcat
错误,这样我就能理解你的应用程序崩溃的原因。我可以编辑我的问题是的,但我有大约100多个logcat错误,所以在这个评论部分可能很难阅读所有这些错误,你不这样认为吗?