如何在点击时打开键盘并在用户点击文本时更改光标位置?我确定它很简单,但我已经尝试将focusable
和focusableInTouchMode
设置为true,enabled
,textIsSelectable
,而且没有工作。还尝试在我的代码中使用以下内容:
InputMethodManager im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
im.showSoftInput(typeField, 0);
我的.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/activity_chat"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/softGreen"
android:textColor="@color/darkBlue"
android:windowSoftInputMode="stateAlwaysVisible"
tools:context="com.angelwing.buddyup.ChatActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/softBlue"
android:title="Hey"
app:theme="@style/MyTheme"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:ems="10"
android:hint="Sample Text"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="@+id/sendButton"
android:layout_toStartOf="@+id/sendButton"
android:paddingLeft="5dp"
android:id="@+id/typeField"/>
<Button
android:layout_width="50dp"
android:layout_height="wrap_content"
android:src="@drawable/edittext_border"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignTop="@+id/typeField"
android:clickable="true"
android:onClick="send"
android:id="@+id/sendButton" />
<ToggleButton
android:textOn="Sun"
android:textOff="Sun"
android:layout_width="@dimen/weekend_toggle_button_width"
android:layout_height="wrap_content"
android:layout_below="@+id/toolbar"
android:id="@+id/sundayButton" />
<ToggleButton
android:textOn="M"
android:textOff="M"
android:layout_width="@dimen/weekday_toggle_button_width"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/sundayButton"
android:layout_toRightOf="@+id/sundayButton"
android:layout_toEndOf="@+id/sundayButton"
android:layout_marginRight="-3dp"
android:layout_marginLeft="-3dp"
android:id="@+id/mondayButton" />
<ToggleButton
android:textOn="T"
android:textOff="T"
android:layout_width="@dimen/weekday_toggle_button_width"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/mondayButton"
android:layout_toRightOf="@+id/mondayButton"
android:layout_toEndOf="@+id/mondayButton"
android:layout_marginRight="-3dp"
android:id="@+id/tuesdayButton" />
<ToggleButton
android:textOn="W"
android:textOff="W"
android:layout_width="@dimen/weekday_toggle_button_width"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/tuesdayButton"
android:layout_toRightOf="@+id/tuesdayButton"
android:layout_toEndOf="@+id/tuesdayButton"
android:layout_marginRight="-3dp"
android:id="@+id/wednesdayButton" />
<ToggleButton
android:textOn="Th"
android:textOff="Th"
android:layout_width="@dimen/weekday_toggle_button_width"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/wednesdayButton"
android:layout_toRightOf="@+id/wednesdayButton"
android:layout_toEndOf="@+id/wednesdayButton"
android:layout_marginRight="-3dp"
android:id="@+id/thursdayButton" />
<ToggleButton
android:textOn="F"
android:textOff="F"
android:layout_width="@dimen/weekday_toggle_button_width"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/thursdayButton"
android:layout_toRightOf="@+id/thursdayButton"
android:layout_toEndOf="@+id/thursdayButton"
android:layout_marginRight="-3dp"
android:id="@+id/fridayButton" />
<ToggleButton
android:textOn="Sat"
android:textOff="Sat"
android:layout_width="@dimen/weekend_toggle_button_width"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/fridayButton"
android:layout_toRightOf="@+id/fridayButton"
android:layout_toEndOf="@+id/fridayButton"
android:layout_marginRight="3dp"
android:id="@+id/saturdayButton" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_below="@+id/sundayButton"
android:layout_above="@+id/edittext"
android:id="@+id/messageListView"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set"
android:textSize="13dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_below="@+id/toolbar"
android:layout_alignBottom="@+id/saturdayButton"
android:layout_toRightOf="@+id/saturdayButton"
android:layout_toEndOf="@+id/saturdayButton"
android:layout_marginLeft="2dp"
android:layout_marginRight="4dp"
android:id="@+id/setButton" />
</RelativeLayout>
我的java文件:
package com.angelwing.buddyup;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
public class ChatActivity extends AppCompatActivity {
Toolbar bar;
SharedPreferences sp;
Button sendButton;
EditText typeField;
String otherUserID;
String buddyID;
String buddyName;
String thisUserName;
String thisUserID;
ChatArrayAdapter adapter;
ArrayList<Message> allMessages;
DatabaseReference convoRef;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
// sendButton = (Button) findViewById(R.id.sendButton);
// typeField = (EditText) findViewById(R.id.typeField);
//
// View view = this.getCurrentFocus();
// if (view != null)
// {
// InputMethodManager im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
// im.showSoftInputFromInputMethod(view.getWindowToken(), 0);
// }
//
// sp = getSharedPreferences("com.angelwing.buddyup", Context.MODE_PRIVATE);
//
// Bundle buddyInfo = getIntent().getExtras();
//
// otherUserID = buddyInfo.getString("otherUserID");
// buddyID = buddyInfo.getString("buddyID");
// buddyName = buddyInfo.getString("buddyName");
//
// thisUserID = buddyInfo.getString("thisUserID");
// thisUserName = buddyInfo.getString("thisUserName");
//
// bar = (Toolbar) findViewById(R.id.toolbar);
// setSupportActionBar(bar);
// getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
// getSupportActionBar().setTitle(buddyName);
// Get allMessages from "Conversations" -> buddyID
// allMessages = new ArrayList<>();
// convoRef = FirebaseDatabase.getInstance().getReference("Conversations").child(buddyID);
//
// ListView messagesList = (ListView) findViewById(R.id.messageListView);
// adapter = new ChatArrayAdapter(getApplicationContext(), allMessages);
// messagesList.setAdapter(adapter);
//
// // Add new messages to allMessages
// convoRef.addChildEventListener(new ChildEventListener() {
// @Override
// public void onChildAdded(DataSnapshot dataSnapshot, String s) {
//
// allMessages.add(dataSnapshot.getValue(Message.class));
// adapter.notifyDataSetChanged();
// }
//
// @Override
// public void onChildChanged(DataSnapshot dataSnapshot, String s) {
//
// }
//
// @Override
// public void onChildRemoved(DataSnapshot dataSnapshot) {
//
// }
//
// @Override
// public void onChildMoved(DataSnapshot dataSnapshot, String s) {
//
// }
//
// @Override
// public void onCancelled(DatabaseError databaseError) {
//
// }
// });
}
public void send(View view)
{
Log.i("Clicked", "Yuppers");
// String chat = typeField.getText().toString();
// chat = truncate(chat);
//
// Message newMessage = new Message(thisUserName, chat, thisUserID);
// convoRef.push().setValue(newMessage);
//
// typeField.setText("");
}
public String truncate (String str)
{
return str;
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.mymenu, menu);
return true;
}
public boolean profileClicked (MenuItem item)
{
Intent intent = new Intent(getApplicationContext(), ProfileScreen.class);
startActivity(intent);
return true;
}
public boolean settingsClicked (MenuItem item)
{
return true;
}
public boolean signOutClicked (MenuItem item)
{
sp.edit().putBoolean("signedIn", false).apply();
// auth.signOut();
Intent intent = new Intent(getApplicationContext(), SignInScreen.class);
startActivity(intent);
return true;
}
}
我的活动扩展了AppCompatActivity,如果这很重要的话。
单击后退按钮使键盘消失后,当我在EditText中单击时,我无法重新出现。我无法做的另一件事是当我点击已经写好的字符串时移动光标所以如果我搞砸了某个地方,我必须删除文本并重新输入。这真是令人沮丧,因为我在另一个文件中有一个EditText工作正常,我没有做任何特别的事情。我第一次进入活动时打开了键盘,这很棒。
我对Android开发相当陌生,所以如果可能的话,请简单地说一下。非常感谢你!
答案 0 :(得分:0)
试试这段代码,它可以帮助您解决问题......
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
public class AndroidExternalFontsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInputFromInputMethod(view.getWindowToken(), 0);
}
}
}