【问题标题】:Android - How to Get list of Emails that are available in gmail app when i want to enter email id in an edittext?Android - 当我想在编辑文本中输入电子邮件 ID 时,如何获取 gmail 应用程序中可用的电子邮件列表?
【发布时间】:2018-02-22 12:32:53
【问题描述】:

当我想在编辑文本中输入电子邮件 ID 时,如何获取 Gmail 应用程序中可用的电子邮件列表?

【问题讨论】:

标签: android email android-edittext


【解决方案1】:

你不能通过Edittext来实现,你应该使用AutoCompleteTextView

<AutoCompleteTextView
android:id="@+id/autoCompleteTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>

在 onCreate 中的 Activity 文件中

    AutoCompleteTextView autoComplete = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView); 

    String selectedEmail;
    List<String> emails = new ArrayList<>();
    Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+
    //get runtime permission

    Account[] accounts = AccountManager.get(YourActivityName.this).getAccounts();

    int i=0;
    for (Account account : accounts) {
        if (emailPattern.matcher(account.name).matches()) {
            String possibleEmail = account.name+"\n";
            emails.add(possibleEmail);
        }
    }

   ArrayAdapter<String> adapter = new ArrayAdapter<String>
   (this,android.R.layout.simple_list_item_1,emails);
   autoComplete.setAdapter(adapter);

   //default selected email
   selectedEmail = emails.get(0);
   autoComplete.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            // You can get the user selected email here    
            String selectedEmail = emails.get(position)
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

您需要在清单中添加权限

<uses-permission android:name="android.permission.GET_ACCOUNTS" />

如果您正在开发 API>=23(Marshmallow),那么您还需要获得运行时权限。按照此获取运行时权限

https://developer.android.com/training/permissions/requesting.html

【讨论】:

  • 没有@Shriyansh Gautam。还没有。
  • 这是我的edittext EditText ed3 = (EditText) findViewById(R.id.emailEditText);字符串 e3 = ed3.getText().toString();而不是你的代码
  • 这没有显示任何错误,但是当我点击手机中的edittext时它不起作用。
  • 它不工作。但是你能帮我完成学习 Android Validations 的课程吗?谢谢。
猜你喜欢
  • 2019-10-12
  • 2021-03-04
  • 2017-01-21
  • 1970-01-01
  • 1970-01-01
  • 2018-11-03
  • 1970-01-01
  • 2018-10-09
  • 2019-02-10
相关资源
最近更新 更多