0
// This is in the class which calls the next activity with an intent:
Bundle bundle = new Bundle();
bundle.putCharSequence("Hint", "test");
startActivityForResult(new Intent(this, PosAct.class)
.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
.putExtras(bundle)
, request_code);

// This is in the PosAct activity which is being called by the class above:
// 1 - Fails!
CharSequence temp = bundle.getCharSequence("Hint");
((EditText) findViewById(R.id.editTextFloor)).setHint(temp);

// 2 - Fails!
((EditText) findViewById(R.id.editTextRoom))
.setHint((CharSequence)bundle.getCharSequence("Hint"));

Toast.makeText(getApplicationContext(), bundle.getCharSequence("Hint"), Toast.LENGTH_LONG).show();

// 3 - Works perfectly!
((EditText) findViewById(R.id.editTextStreet)).setHint("test");

xml の代わりに Java コードを使用してヒント テキストを設定しようとしましたが、setHint() メソッドの引数として変数を使用できませんでした。上記では、3 つの異なる EditText で試行を行います。

上記の番号 1 は機能しません。EditText は空のままです。上記の 2 番目の結果は同じですが、トーストは正しく表示されます (「テスト」)。上記の番号 3 は完全に機能します。EditText にはヒント テキストとして「test」があります。

最初に String を使用して同じ結果を試しました。CharSequence を明示的に使用しても役に立ちませんでした。これは何ですか?

4

1 に答える 1

1

これを試して..

 Intent intent = new Intent(this, PosAct.class)
.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);


        //load the intent with a key "hint" and assign it's value
        //to be whatever has been entered into the text field...
        intent.putExtra("hint","test");

あなたの活動では、これを使用するために渡しています..

    Bundle extras = intent.getExtras();
    mEditText1.setHint(extras != null ? extras.getString("hint"):"nothing 
于 2012-02-03T20:10:21.417 に答える