活动开始时如何隐藏EditText光标

时间:2019-09-02 18:41:50

标签: android

活动开始时,EditText会自动放置光标而不显示键盘。

当我完成使用EditText并隐藏键盘时,光标仍然显示。

import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'models/model.dart';
import 'models/card.dart';
import 'article.dart';

final API_KEY = '***';

Future<List<Source>> fetchNewsSource() async {
  final response = await http.get(
      'https://newsapi.org/v2/sources?language=en&country=in&apiKey=$API_KEY');

  if (response.statusCode == 200) {
    List sources = json.decode(response.body)['sources'];
    return sources.map((source) => new Source.formJson(source)).toList();
  } else {
    throw Exception('Fail to load data');
  }
}

class SourceScreen extends StatefulWidget {
  @override
  _SourceScreenState createState() => _SourceScreenState();
}

class _SourceScreenState extends State<SourceScreen> {
  var list_source;
  var refreshKey = GlobalKey<RefreshIndicatorState>();

  @override
  void initState() {
    super.initState();
    refreshListSource();
  }

  Future<Null> refreshListSource() async {
    refreshKey.currentState?.show(atTop: false);
    setState(() {
      list_source = fetchNewsSource();
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        backgroundColor: Color.fromRGBO(58, 66, 86, 1.0),
        appBar: AppBar(
          elevation: 1.0,
          backgroundColor: Color.fromRGBO(58, 66, 86, 1.0),
          title: Text('uTTerNews'),
        ),
        body: Center(
          child: RefreshIndicator(
              child: FutureBuilder<List<Source>>(
                future: list_source,
                builder: (context, snapshot) {
                  if (snapshot.hasError) {
                    Text('Error: ${snapshot.error}');
                  } else if (snapshot.hasData) {
                    List<Source> sources = snapshot.data;
                    return new ListView(
                        children: sources
                            .map((source) =>
                            GestureDetector(
                              onTap: () {
                                Navigator.push(context, MaterialPageRoute(
                                    builder: (context) =>
                                        articleScreen(source: source,)));
                              },
                              child: card(source),
                            ))
                            .toList());
                  }
                  return CircularProgressIndicator();
                },
              ),
              onRefresh: refreshListSource),
        ),
      ),
    );
  }
}

2 个答案:

答案 0 :(得分:0)

应对

解决方案

1)在您的xml中的EditText下设置:

android:cursorVisible="false"

2)设置onClickListener:

iEditText.setOnClickListener(editTextClickListener);

OnClickListener editTextClickListener = new OnClickListener() 
{ public void onClick(View v) 
{  if (v.getId() == iEditText.getId()) 
    {
        iEditText.setCursorVisible(true);
    }

}
};

3)然后是onCreate,使用OnEditorActionListener将其按下后捕获的事件捕获到您的EditText,然后setCursorVisible(false)。

iEditText.setOnEditorActionListener(new OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView v, int actionId,
                KeyEvent event) {
                iEditText.setCursorVisible(false);
            if (event != null&& (event.getKeyCode() == 
KeyEvent.KEYCODE_ENTER)) {
                InputMethodManager in = (InputMethodManager) 
getSystemService(Context.INPUT_METHOD_SERVICE);

in.hideSoftInputFromWindow(iEditText.getApplicationWindowToken()
,InputMethodManager.HIDE_NOT_ALWAYS);
            }
            return false;
        }
    });

答案 1 :(得分:0)

如果您需要以编程方式从edittext中删除光标,则可以使用

edit_search.clearFocus();

这会将光标移动到布局中的下一个可聚焦项。