我正在做一个简单的聊天客户端,希望能够在我的editText中编写一条消息,而无需关闭软键盘,然后点击旁边的" Send" -button editText并且完全可见。目前无法做到这一点。
MY XML
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/connect"
android:id="@+id/connectButton"
android:layout_gravity="right|top"
android:onClick="ConnectButtonClicked"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="@string/send"
android:id="@+id/sendButton"
android:enabled="false"
android:layout_gravity="right|bottom"
android:onClick="SendButtonClicked"/>
<EditText
android:layout_width="228dp"
android:layout_height="wrap_content"
android:ems="10"
android:id="@+id/messageEditText"
android:layout_gravity="left|bottom"
android:inputType="textLongMessage" />
<EditText
android:layout_width="190dp"
android:layout_height="50dp"
android:ems="10"
android:id="@+id/usernameEditText"
android:layout_gravity="left|top"
android:inputType="textPersonName" />
<TextView
android:layout_width="match_parent"
android:layout_height="391dp"
android:text=""
android:id="@+id/chatTextView"
android:layout_gravity="center"
android:layout_marginTop="100dp"
android:layout_marginBottom="100dp"
android:maxLines="100"
android:scrollbars="vertical"/>
</FrameLayout>
</RelativeLayout>
相关课程代码
public class ChatClientMainActivity extends ActionBarActivity
{
Socket socket;
private ServerListener serverListener;
private Thread listenerThread;
public Handler messageHandler;
private TextView chatTextView;
private EditText usernameEditText;
private EditText messageEditText;
private Button sendButton;
private Button connectButton;
private int PORT = 2001;
private String hostAddress = "192.168.1.3";
private static class HandlerExtension extends Handler
{
private final WeakReference<ChatClientMainActivity> currentActivity;
public HandlerExtension(ChatClientMainActivity activity){
currentActivity = new WeakReference<ChatClientMainActivity>(activity);
}
@Override
public void handleMessage(Message msg)
{
ChatClientMainActivity UIActivity = currentActivity.get();
if (msg != null)
{
Bundle tempBundle = msg.getData();
UIActivity.PrintMessage(tempBundle.getString("chatMessage"));
}
else
{
// ALERT MESSAGE
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat_client_main);
messageHandler = new HandlerExtension(this);
chatTextView = (TextView) findViewById(R.id.chatTextView);
chatTextView.setMovementMethod(new ScrollingMovementMethod());
connectButton = (Button) findViewById(R.id.connectButton);
sendButton = (Button) findViewById(R.id.sendButton);
usernameEditText = (EditText) findViewById(R.id.usernameEditText);
messageEditText = (EditText) findViewById(R.id.messageEditText);
messageEditText.setOnKeyListener(new View.OnKeyListener(){
@Override
public boolean onKey(View v, int keyCode, KeyEvent event){
if(keyCode == event.KEYCODE_ENTER){
SendButtonClicked(v);
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(messageEditText.getWindowToken(), 0);
return true;
}
return false;
}
});
//InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
//imm.SoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
}
public void SendButtonClicked(View view)
{
String message = messageEditText.getText().toString();
if (!message.equals(""))
ServerListener.SendMessage(message);
messageEditText.setText("");
}
public void ConnectButtonClicked(View view)
{
if (connectButton.getText().toString().equals("Connect"))
{
if (usernameEditText.getText().toString() != "")
{
if (usernameEditText.getText().length() <= 10)
{
String username = usernameEditText.getText().toString();
listenerThread = new Thread(new ServerListener(this, "192.168.1.3", username, messageHandler));
listenerThread.start();
sendButton.setEnabled(true);
connectButton.setText("Disconnect");
usernameEditText.setEnabled(false);
PrintMessage("Welcome to the chat!");
}
else
{
// "Username max length is 10 characters"
//
// Dialog dialog = new Dialog(null);
// TextView txt = (TextView)dialog.findViewById(R)
}
}
else
{
// "Enter a username!"
// Dialog dialog = new Dialog(null);
}
}
else if (connectButton.getText().toString() == "Disconnect")
{
QuitChat("Disconnect");
sendButton.setEnabled(false);
connectButton.setText("Connect");
usernameEditText.setText("");
usernameEditText.setEnabled(true);
// listenerThread.a
}
}
public void PrintMessage(String lineToPrint)
{
Time now = new Time();
now.setToNow();
String timeString = now.format("%H:%M ");
final String line = timeString + ": " + lineToPrint + "\n";
messageHandler.post(new Runnable()
{
@Override
public void run()
{
chatTextView.setText(chatTextView.getText() + line);
chatTextView.invalidate();
}
});
}
public void QuitChat(String input) {
ServerListener.SendMessage("LOGOUT");
//Quit chat
}
}
指向我的java-和xml文件的链接以及该情况的屏幕截图: https://www.dropbox.com/sh/8dfbq6c2xjkoqi5/AAA726NdFj5EjoyLyE94ecPWa?dl=0
iOS有一个相同的问题,但没有适用的答案。 Can't click button when soft-keyboard is visible
有什么想法吗?提前致谢!
解决 问题是由我的布局xml中的这一行引起的: &#34;机器人:滚动条=&#34;垂直&#34; /&GT;&#34 ;. 我是Android编程的新手,所以如果有人可以解释为什么那会很棒,我会接受这个作为答案!