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");

我尝试使用 java 代码而不是 xml 设置提示文本,但我没有使用任何变量作为 setHint() 方法的参数。上面我尝试了 3 种不同的 EditText。

上面的 Nr 1 不起作用。EditText 保持为空。上面的 Nr 2 具有相同的结果,但 Toast 显示正确(“测试”)。上面的 Nr 3 完美地工作,EditText 有“测试”作为它的提示文本。

我首先尝试使用具有相同结果的字符串。明确使用 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 回答