我想将操作栏标题文本更改为在editText视图中键入的任何内容?
到目前为止,我有这个:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
editTextHouse = findViewById(R.id.edit_text_house);
ActionBar actionBar = getSupportActionBar();
getSupportActionBar().setTitle(editTextHouse);
String title = actionBar.getTitle().toString(); ```
答案 0 :(得分:2)
您好,您可以通过Button Click尝试
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stack);
Button buttonClick = findViewById(R.id.buttonClick);
final EditText editTextData = findViewById(R.id.editTextData);
buttonClick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//this require non null for null string crash
Objects.requireNonNull(getSupportActionBar()).setTitle(editTextData.getText().toString());
}
});
}
答案 1 :(得分:0)
在Edittext上的textChang actionBar标题将被更改,请尝试此操作。
editTextHouse .addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// Do some thing now
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
// Do something at this time
if (s != null && s.length() > 0)
getSupportActionBar().setTitle(s.toString());
}
});
答案 2 :(得分:0)
使用getSupportActionBar()。setTitle(editTextHouse.getText());为了你在做什么
答案 3 :(得分:0)
editTextHouse = findViewById(R.id.edit_text_house);
String text = editTextHouse.getText().toString();
actionBar.setTitle(text);
将文本从可编辑文本转换为字符串,然后将该字符串设置为操作栏的标题。