我正在为Android开发一个聊天应用程序。它具有用于显示聊天的列表视图,用于输入文本的EditText和用于发送文本的按钮。每当启动活动时,键盘就会自动出现。但是,当我通过按“后退”按钮取消键盘时,单击EditText时键盘不会重新出现。我尝试将focusable
属性添加到edittext,但是没有用。请告诉我我在做什么错。
XML文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@+id/main_activity"
>
<android.support.design.widget.FloatingActionButton
android:id="@+id/floatingActionButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="12dp"
android:clickable="true"
android:focusable="true"
app:backgroundTint="@color/colorPrimary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="@android:drawable/ic_menu_send" />
<EditText
android:id="@+id/editText"
android:layout_width="260dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:ems="10"
android:hint="Type here"
android:focusable="true"
android:focusableInTouchMode="true"
android:inputType="textCapSentences"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/floatingActionButton3"
app:layout_constraintHorizontal_bias="0.472"
app:layout_constraintStart_toStartOf="parent" />
<ListView
android:id="@+id/messages"
android:layout_width="395dp"
android:layout_height="647dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="@+id/floatingActionButton3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
ActivityMain.java文件:
public class MainActivity extends AppCompatActivity {
private static int SIGN_IN_REQUEST_CODE = 1;
private FirebaseListAdapter<ChatMessage> adapter;
RelativeLayout mainActivity;
FloatingActionButton fab;
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.sign_out_menu)
{
AuthUI.getInstance().signOut(this).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Snackbar.make(mainActivity,"Signed out successfully",Snackbar.LENGTH_SHORT).show();
finish();
}
});
}
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu,menu);
return true;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if(requestCode == SIGN_IN_REQUEST_CODE)
{
if(resultCode == RESULT_OK)
{
Snackbar.make(mainActivity,"Welcome " + FirebaseAuth.getInstance().getCurrentUser().getEmail(),Snackbar.LENGTH_SHORT).show();
displayChatMessages();
}
else
{
Snackbar.make(mainActivity,"Login failed.",Snackbar.LENGTH_SHORT).show();
finish();
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fab = findViewById(R.id.floatingActionButton3);
mainActivity = findViewById(R.id.main_activity);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText editText = findViewById(R.id.editText);
FirebaseDatabase.getInstance().getReference().push().setValue(new ChatMessage(
FirebaseAuth.getInstance().getCurrentUser().getEmail(),
editText.getText().toString()
));
editText.setText("");
}
});
if(FirebaseAuth.getInstance().getCurrentUser() == null)
{
startActivityForResult(AuthUI.getInstance().createSignInIntentBuilder().build(),SIGN_IN_REQUEST_CODE);
}
else
{
Snackbar.make(mainActivity,"Welcome back " + FirebaseAuth.getInstance().getCurrentUser().getEmail(),Snackbar.LENGTH_SHORT).show();
displayChatMessages();
}
}
Query query = FirebaseDatabase.getInstance().getReference().child("chats");
FirebaseListOptions<ChatMessage> options = new FirebaseListOptions.Builder<ChatMessage>()
.setQuery(query,ChatMessage.class)
.setLayout(R.layout.list_item)
.build();
private void displayChatMessages()
{
ListView listView = findViewById(R.id.messages);
adapter = new FirebaseListAdapter<ChatMessage>(options) {
@Override
protected void populateView(@NonNull View v, @NonNull ChatMessage model, int position) {
TextView textView = v.findViewById(R.id.username);
TextView textView1 = v.findViewById(R.id.messageText);
TextView textView2 = v.findViewById(R.id.time);
textView.setText(model.getUsername());
textView1.setText(model.getMessageText());
textView2.setText(DateFormat.format("dd-MM-yyyy (HH:mm:ss)",model.getTime()));
}
};
listView.setAdapter(adapter);
}
}
答案 0 :(得分:0)
尝试:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
或
InputMethodManager im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
im.showSoftInput(edittext, 0);