こんにちは、私は Android プログラミングを学ぼうとしています。ここで Android チュートリアルを使用していました。 API ガイドを使用してビットから学びます。
私がやりたかった最初の編集は、最終的なものが表示されたときに別のテキスト行を追加するユーザーがチェックできるチェックボックスを追加することでした. ただし、チェック ボックスをコード (メイン アクティビティ) に追加し、DisplayMessageActivity にデータを送信して、ユーザーが定義した他のテキスト行の下に別のテキスト行を追加するように指示する方法がわかりませんでしたメッセージボックス、助けてください(私が明確に説明していなかったら申し訳ありません)
これまでのところ、2 つのアクティビティのコードが役に立ちましたら、基本的な回答をお願いします。
主な活動:
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);
}
}