gpt4 book ai didi

android - EditText 限制最多 5 个联系人

转载 作者:行者123 更新时间:2023-11-30 03:04:22 25 4
gpt4 key购买 nike

我正在制作一个Panic App,并允许用户添加多个联系人并通过点击添加到联系人将选定的联系人显示到EditText中> 按钮,只要他/她想添加。

在 EditText 中得到这样的东西:9867XXXXXX, 9866XXXXXX, ......

我需要的更改:

限制最多5个联系人

我知道如何在 EditText 中限制字符长度,但不知道如何限制 5 个联系人?

     private Button btnAddContacts ;
private EditText editContacts

..............................
btnAddContacts = (Button) findViewById(R.id.btnAddContacts);
btnAddContacts.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent it= new Intent(Intent.ACTION_GET_CONTENT);
it.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
startActivityForResult(it, 1);
}
});

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data != null) {
Uri uri = data.getData();

if (uri != null) {
Cursor c = null;
try {
c = getContentResolver().query(uri, new String[]{
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.TYPE },
null, null, null);

if (c != null && c.moveToFirst()) {
String number = c.getString(0);
int type = c.getInt(1);
showSelectedNumber(type, number);
}
} finally {
if (c != null) {
c.close();
}
}
}
}
}

public void showSelectedNumber(int type, String number) {

if(editContacts != null && editContacts.getText().toString().length()==0)
editContacts.setText(number);
else
if(editContacts != null) editContacts.append(","+number);

}

最佳答案

你可以使用一个标志说 maxNoContacts

fragment :

public YourActivity extends Activity
{
int maxNoContacts = 0;

public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState)
...
btnAddContacts.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(maxNoContacts <5 )
{
Intent it= new Intent(Intent.ACTION_GET_CONTENT);
it.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
startActivityForResult(it, 1);
}
else
{
//show toast saying you added maximum no of contacts.
}

}
});
}

//如果正确获取了联系人,则更新 maxNoContacts。

  @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data != null) {
Uri uri = data.getData();

if (uri != null) {
Cursor c = null;
try {

c = getContentResolver().query(uri, new String[]{
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.TYPE },
null, null, null);

if (c != null && c.moveToFirst()) {
String number = c.getString(0);
int type = c.getInt(1);

maxNoContacts++; //Increment maxNoContacts if it fetches contact properly.

showSelectedNumber(type, number);
}
} finally {
if (c != null) {
c.close();
}
}
}
}
}



}

关于android - EditText 限制最多 5 个联系人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22059868/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com