gpt4 book ai didi

java - 在android中将2位数据从1个 Activity 发送到另一个 Activity

转载 作者:行者123 更新时间:2023-12-01 04:45:53 25 4
gpt4 key购买 nike

嗨,我正在尝试学习 Android 编程,我正在使用这里的 Android 教程:http://developer.android.com/training/basics/firstapp/index.html但随后我打算编辑它并添加我自己的内容,并通过使用 API 指南向他们学习。

我想做的第一个编辑是添加一个复选框,用户可以选中该复选框,以便在显示最终内容时添加另一行文本。但是我不确定如何将复选框添加到代码(在主 Activity 中)并使其将数据发送到 DisplayMessageActivity 以告诉它在用户定义的另一行文本下添加另一行文本消息框,请帮忙(抱歉,如果我没有解释清楚)

这是到目前为止 2 个 Activity 的代码,如果有帮助的话,请回答基本问题,我已经有一段时间没有编程了,而且我以前使用过 java,所以我仍在了解事情的进展。

主要 Activity :

package com.hyper.benshelloworld;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.Menu;
import android.widget.CheckBox;
import android.widget.EditText;
/*
* private things can be directly accessed only within the class that defines them.
*•default (i.e., no access modifier) things can be accessed by any code in the same package as the defining class.
*•protected is like default, except subclasses outside of the defining package can also access the member.
*•public members are accessible to all code everywhere.
*/

public class MainActivity extends Activity {
public static final String EXTRA_MESSAGE = "com.hyper.benshelloworld.MESSAGE";
//defines the string EXTRA_MESSAGE

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

@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;
}
public void sendMessage(View view){
Intent intent = new Intent(this, DisplayMessageActivity.class);
//calls on DisplayMessageActivity.class
EditText editText = (EditText) findViewById(R.id.EditText);
//makes the text box writable in.
String message = editText.getText().toString();
/*receives the typed message and makes it ready to be sent to the
* DisplayMessageActivity to be displayed*/
intent.putExtra(EXTRA_MESSAGE, message);
/*The most common use of intents is to start new activities (screens)within
* an application (line 41). The extras Bundle is a way of passing data between activities.
* Extras are entered as key value pairs so EXTRA_MESSAGE is a key is used to identify a
* particular value so it can be retrieved and used by another activity.
*/

startActivity(intent);




}


}

和 DisplayMessageActivity:

package com.hyper.benshelloworld;
/*
* private things can be directly accessed only within the class that defines them.
*•default (i.e., no access modifier) things can be accessed by any code in the same package as the defining class.
*•protected is like default, except subclasses outside of the defining package can also access the member.
*•public members are accessible to all code everywhere.
*/

import android.os.Build;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.support.v4.app.NavUtils;
import java.lang.String;

public class DisplayMessageActivity extends Activity {

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Intent intent = getIntent();
//receives the intent sent in the MainActivity.class
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
//Uses the getStringExtra to receive the string message from MainActivity in the intent.


TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
/*Sets up a text view to show the string "message" in and
*sets the size of the text
*/

if (message.equals("hello world")){
textView.setText("bit standard");
}

setContentView(textView);
//sets the activity to be showing the textView

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
/* If the android version is 3.0+,
* show the Up button in the action bar.
*/
getActionBar().setDisplayHomeAsUpEnabled(true);
}

}


@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_display_message, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}

}

最佳答案

您可以将多个内容放入附加项中:

public class MainActivity extends Activity {
// ...
public void sendMessage(View view){
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.EditText);
String message = editText.getText().toString();
Bundle bundle = new Bundle();
bundle.putString(DisplayMessageActivity.KEY_BUNDLE_SOME_VALUE, message);
bundle.putString(DisplayMessageActivity.KEY_BUNDLE_SOME_OTHER_VALUE, someOtherValue);
intent.putExtras(bundle);
startActivity(intent);
}
// ...
}

在接收 Activity 中:

public class DisplayMessageActivity extends Activity {

public static final String KEY_BUNDLE_SOME_VALUE = "some_value";
public static final String KEY_BUNDLE_SOME_OTHER_VALUE = "some_other_value";

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getIntent().getExtras();
String string1 = "";
String string2 = "";
if (bundle != null) {
if (bundle.containsKey(KEY_BUNDLE_SOME_VALUE) {
string1 = bundle.getString(KEY_BUNDLE_SOME_VALUE);
}
if (bundle.containsKey(KEY_BUNDLE_SOME_OTHER_VALUE) {
string2 = bundle.getString(KEY_BUNDLE_SOME_OTHER_VALUE);
}
}
// ...
}
// ...
}

最好在 bundle key 的接收 Activity 中添加公共(public)常量。这将有助于防止您因拼写错误而犯错误:)并且如果您决定更新它们,它们将位于 1 位。

关于java - 在android中将2位数据从1个 Activity 发送到另一个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15854645/

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