简体   繁体   中英

Keyboard not showing up when EditText is clicked

I've created a method that is called when my EditText is clicked (or touched, to be specific), which simply takes the current time and displays it exactly as it has been passed by System.currentTimeMillis() in a TextView below. The method is this one:

public void captureTime(View view) {
        currentTime = System.currentTimeMillis();
        String currentTimeStr = currentTime+ "";
        TextView textView = findViewById(R.id.textView);
        textView.setText(currentTimeStr);
}

To do that, I've had to add the code below in the onCreate method. It uses setOnTouchListener instead of setOnClickListener since otherwise the captureTime method wasn't always called. Now, this method does what I wanted, but now when I click or touch the EditText, although it does this as required, the keyboard doesn't show up anymore. I have looked into other questions in which the keyboard doesn't show up either but they're not related to my case, although I have tried them. So, what is that is making the keyboard not show up and how can I fix this?

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        add_time = (EditText)findViewById(R.id.editText);
        add_time.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_UP) {
                    captureTime(add_time);
                    return true;
                }
                return false;
            }
        });
    }

Try removing return true (in the if block). In this case, the "captureTime" method will work and the keyboard will open.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM