从textView中删除所有非数字值

时间:2016-09-17 08:40:28

标签: android textview autocompletetextview

在我的应用程序上测试后,我在TextView字段中获得数值(电话)和字符串值(名称)。我想删除所有非数字值。

  public class SendingSMSActivity extends Activity{
private int mCount;
private AutoCompleteTextView actv;
private ArrayList<String> mcurrentssFistLastName_PhoneList = null;
AutoCompleteTextView PhoneNumberAutoComp;
MultiAutoCompleteTextView PhoneNumberMultiAutoComp;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sending_sms_activity);
    PhoneNumberAutoComp=(AutoCompleteTextView)findViewById(R.id.aCT_phone_number);
    PhoneNumberMultiAutoComp=(MultiAutoCompleteTextView)findViewById(R.id.multiCompTextView);

    ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,init());

    PhoneNumberAutoComp.setAdapter(adapter);
    PhoneNumberAutoComp.setThreshold(1);

    PhoneNumberMultiAutoComp.setAdapter(adapter);
    PhoneNumberMultiAutoComp.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
}

private ArrayList<String> init(){
    mcurrentssFistLastName_PhoneList = new ArrayList<String>();
    //mcurrentssFistLastNameList = new ArrayList<String>();
    InnerDB db = new InnerDB(SendingSMSActivity.this);

    final ArrayList<String> sPhoneList = db.getAllPhoneNumber();
    final ArrayList<String> sFirstLastNameList = db.getAllSFirstName();
    mCount = sPhoneList.size();
    for(int i = 0; i < mCount; i ++){
        mcurrentssFistLastName_PhoneList.add(sFirstLastNameList.get(i) +" " +sPhoneList.get(i));
        //mcurrentssFistLastNameList.add(sFirstLastNameList.get(i));
    }

    return mcurrentssFistLastName_PhoneList;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    return super.onOptionsItemSelected(item);
}

@Override
protected void onResume() {
    super.onResume();
}
}

2 个答案:

答案 0 :(得分:1)

根据@PrateekRathore的反馈,我得到了解决方案:

String phonevalue =  PhoneNumberAutoComp.getText().toString();
            String phoneNum =phonevalue.replaceAll("[^0-9]","");
            PhoneNumberAutoComp.setText(phoneNum);

答案 1 :(得分:0)

您可以随时将其替换为replaceAll(“[^ 0-9]”,“”); 。这只会返回数字值。