有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

类似facebook messenger的java聊天头,带有编辑文本,但编辑文本不显示在键盘上

我已经使用该服务为我的应用程序实现了类似于弹出式聊天头的功能。我在里面使用了编辑文本

但问题是,当我点击编辑文本时,只有光标是可见的,键盘并没有出现。甚至无法选择或复制或粘贴编辑文本中的文本。有什么帮助吗?谢谢,这是我的密码

import 安卓.annotation.SuppressLint;
import 安卓.app.Notification;
import 安卓.app.NotificationChannel;
import 安卓.app.NotificationManager;
import 安卓.app.Service;
import 安卓.content.Context;
import 安卓.content.Intent;
import 安卓.graphics.Color;
import 安卓.graphics.PixelFormat;
import 安卓.os.Build;
import 安卓.os.IBinder;
import 安卓.transition.AutoTransition;
import 安卓.transition.TransitionManager;
import 安卓.view.Gravity;
import 安卓.view.LayoutInflater;
import 安卓.view.MotionEvent;
import 安卓.view.View;
import 安卓.view.WindowManager;
import 安卓.view.inputmethod.InputMethodManager;
import 安卓.widget.EditText;
import 安卓.widget.ImageView;
import 安卓.widget.LinearLayout;

import 安卓x.annotation.RequiresApi;
import 安卓x.cardview.widget.CardView;
import 安卓x.core.app.NotificationCompat;

public class NotePopUpHead extends Service {

    private WindowManager mWindowManager;
    private View mFloatingView;
    EditText editText;


    private WindowManager.LayoutParams params;

    public NotePopUpHead() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public void onCreate() {
        super.onCreate();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
            startMyOwnForeground();
        else
            startForeground(1, new Notification());
        //Inflate the floating view layout we created
        mFloatingView = LayoutInflater.from(this).inflate(R.layout.bubble, null);
         editText = mFloatingView.findViewById(R.id.Email1);

        int LAYOUT_FLAG;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
            params = new WindowManager.LayoutParams(
                    WindowManager.LayoutParams.WRAP_CONTENT,
                    WindowManager.LayoutParams.WRAP_CONTENT,
                    LAYOUT_FLAG,
                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                    PixelFormat.TRANSLUCENT);
        } else {
            LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_PHONE;
            params = new WindowManager.LayoutParams(
                    WindowManager.LayoutParams.WRAP_CONTENT,
                    WindowManager.LayoutParams.WRAP_CONTENT,
                    LAYOUT_FLAG,
                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                    PixelFormat.TRANSLUCENT);
        }
        
//        Specify the view position
        params.gravity = Gravity.TOP | Gravity.LEFT;        //Initially view will be added to top-left corner
        params.x = 0;
        params.y = 100;

        //Add the view to the window
        mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        mWindowManager.addView(mFloatingView, params);


        //Set the close button
        ImageView closeButtonCollapsed = (ImageView) mFloatingView.findViewById(R.id.close_btn);
        closeButtonCollapsed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //close the service and remove the from from the window
                stopSelf();
            }
        });

        mFloatingView.findViewById(R.id.collapse_view).setOnTouchListener(new View.OnTouchListener() {
            private int initialX;
            private int initialY;
            private float initialTouchX;
            private float initialTouchY;


            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:

                        //remember the initial position.
                        initialX = params.x;
                        initialY = params.y;

                        //get the touch location
                        initialTouchX = event.getRawX();
                        initialTouchY = event.getRawY();
                        return true;
                    case MotionEvent.ACTION_UP:
                        int Xdiff = (int) (event.getRawX() - initialTouchX);
                        int Ydiff = (int) (event.getRawY() - initialTouchY);


                        //The check for Xdiff <10 && YDiff< 10 because sometime elements moves a little while clicking.
                        //So that is click event.
                        if (Xdiff < 10 && Ydiff < 10) {
                            ImageView imageView = mFloatingView.findViewById(R.id.show_more);
                            imageView.setOnClickListener(new View.OnClickListener() {
                                @RequiresApi(api = Build.VERSION_CODES.KITKAT)
                                @Override
                                public void onClick(View view) {
                                    CardView cardView = mFloatingView.findViewById(R.id.collapse_view);
                                    LinearLayout linearLayout = mFloatingView.findViewById(R.id.expanded);
                                    if(linearLayout.getVisibility() == View.VISIBLE){
                                        TransitionManager.beginDelayedTransition(cardView,
                                                new AutoTransition());
                                        linearLayout.setVisibility(View.GONE);

                                    } else{

                                        TransitionManager.beginDelayedTransition(cardView,
                                                new AutoTransition());
                                        linearLayout.setVisibility(View.VISIBLE);
                                        
                                    }

                                }
                            });
                        }
                        return true;
                    case MotionEvent.ACTION_MOVE:
                        //Calculate the X and Y coordinates of the view.
                        params.x = initialX + (int) (event.getRawX() - initialTouchX);
                        params.y = initialY + (int) (event.getRawY() - initialTouchY);


                        //Update the layout with new X & Y coordinate
                        mWindowManager.updateViewLayout(mFloatingView, params);
                        return true;
                }
                return false;
            }
        });
    }
  


    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mFloatingView != null) mWindowManager.removeView(mFloatingView);
    }

    private void startMyOwnForeground() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            String NOTIFICATION_CHANNEL_ID = "com.example.simpleapp";
            String channelName = "My Background Service";
            NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
            chan.setLightColor(Color.BLUE);
            chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
            NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            assert manager != null;
            manager.createNotificationChannel(chan);

            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
            Notification notification = notificationBuilder.setOngoing(true)
                    .setContentTitle("App is running in background")
                    .setPriority(NotificationManager.IMPORTANCE_MIN)
                    .setCategory(Notification.CATEGORY_SERVICE)

                    .build();

            startForeground(2, notification);
        }
    }
}

共 (1) 个答案

  1. # 1 楼答案

    考虑添加您的^ {< CD1>}{{CD2}}方法:

    InputMethodManager imm = InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.showSoftInput(targetEditText, InputMethodManager.SHOW_IMPLICIT);
    

    也可以在EditText上调用requestFocus()

    要隐藏键盘,请执行以下操作:

    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    

    所需进口:

    import android.content.Context;
    import android.view.inputmethod.InputMethodManager;