0

2 番目のアクティビティで前のアクティビティのユーザー名を表示する方法がわかりません。

たくさんの方法を試しましたが、文字列にユーザー名を表示する方法はありません

これは MainActivity です:

       private EditText nameField;


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

    nameField = findViewById(R.id.nameEditText);
    Button startButton = findViewById(R.id.startButton);
    startButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String name = nameField.getText().toString();
            getusername(name);
        }
    });

}

private void getusername(String name) {
    Intent intent = new Intent(this, OneShot.class);
    Resources resources = getResources();
    String key = resources.getString(R.string.key_name);
    intent.putExtra(key, name);
    startActivity(intent);
}

これは SecondActivity です:

 public static final String TAG = OneShot.class.getSimpleName();
private String name;
private int nextSentenceId = 0;

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

    Intent intent = getIntent();
    name = intent.getStringExtra(getString(R.string.key_name));
    if (name == null || name.isEmpty()) {
        name = "Player";
    }
    Log.d(TAG, name);

    final int[] sentences = new int[]{R.string.page0, R.string.page2, R.string.page3, R.string.page4, R.string.page5 };

    Button next= findViewById(R.id.next);
    final TextView displayText= findViewById(R.id.displayText);

    next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final int limit = 5;
            if(nextSentenceId < sentences.length){
                displayText.setText(sentences[nextSentenceId]);
                ++nextSentenceId;
            }
        }
    });

}

これらは int[] の文字列です:

     <string name="page0"> Hello %1$s </string>
<string name="page2"> Welcome into this application </string>
<string name="page3"> How are you ? </string>
<string name="page4"> How is the weather </string>
<string name="page5"> Is %1$s you\'re real name ? </string>

ユーザー名が文字列文で渡されないのはなぜですか?どうすれば修正できますか? 前もって感謝します。

4

2 に答える 2