如何从EditText获取文本并在ActionBar上显示?

时间:2016-08-12 07:21:40

标签: android android-edittext android-actionbar fragment

我有EditText,用户在其中输入他们的帐号。现在我想要的是当用户停止在EditText中键入他的帐号时,我想在ActionBar中显示该帐号。我怎样才能实现这一目标?

2 个答案:

答案 0 :(得分:0)

此代码可能有效:

getSupportActionBar().setTitle(editText.getText().toString());

如果您想在操作栏的右侧设置标题,建议您使用Toolbar来自定义布局。

答案 1 :(得分:0)

尝试使用TextWatcher。像这样:

editText.addTextChangedListener(new TextWathcer(){
    void beforeTextChanged(CharSequence s, int start, int count, int after){}
    void onTextChanged(CharSequence s, int start, int before, int count){}
    void afterTextChanged(Editable s){
        getSupportActionBar().setTitle(s.getText().toString());
    }
});