我有一个字母和数字字符串。
如下图所示:
我想将数字与字母分开,当用户点击数字时 这些数字在屏幕上显示为按钮。
如下图:
我有一个活动将此字符串带到另一个活动。
基本上我应该怎么做?感谢。
我的活动1:
public class BoxActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.box);
TextView textView = (TextView) findViewById(R.id.txtView);
Bundle bundle = getIntent().getExtras();
if(bundle != null){
String strBox = bundle.getString("fln");
textView.setText(strBox);
}
}
我的活动2
public class SmsInbox extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, AdapterView.OnItemClickListener {
private static SmsInbox inst;
ArrayList<String> smsMessagesList = new ArrayList<String>();
ListView smsListView;
ArrayAdapter arrayAdapter;
public static SmsInbox instance() {
return inst;
}
@Override
public void onStart() {
super.onStart();
inst = this;
}
DrawerLayout drawer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sms_inbox);
smsListView = (ListView) findViewById(R.id.SmsList);
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, smsMessagesList);
smsListView.setAdapter(arrayAdapter);
smsListView.setOnItemClickListener(this);
if(ContextCompat.checkSelfPermission(getBaseContext(), "android.permission.READ_SMS") == PackageManager.PERMISSION_GRANTED) {
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(Uri.parse("content://sms/inbox"), null, null,
null, null);
int indexBody = cursor.getColumnIndex("body");
int indexAddr = cursor.getColumnIndex("address");
if (indexBody < 0 || !cursor.moveToFirst()) return;
arrayAdapter.clear();
do {
String str = "?????? ??: " + cursor.getString(indexAddr) +
"\n" + cursor.getString(indexBody) + "\n";
arrayAdapter.add(str);
} while (cursor.moveToNext());
}
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
toggle.setDrawerIndicatorEnabled(false);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
public void updateList(final String smsMessage) {
arrayAdapter.insert(smsMessage, 0);
arrayAdapter.notifyDataSetChanged();
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
try {
String[] smsMessages = smsMessagesList.get(pos).split("\n");
String address = smsMessages[0];
String smsMessage = "";
for (int i = 1; i < smsMessages.length; ++i) {
smsMessage += smsMessages[i];
}
/* String smsMessageStr = address + "\n";
smsMessageStr += smsMessage;
Toast.makeText(this, smsMessageStr, Toast.LENGTH_SHORT).show();
*/
Intent intent = new Intent(this, BoxActivity.class);
String strBox = smsMessage;
intent.putExtra("fln", strBox);
startActivity(intent);
/*Pattern isnumbers = Pattern.compile("[0-9]+$");
Matcher numberMatch = isnumbers.matcher(strBox);
if(numberMatch.matches()){
Toast.makeText(this, "" + numberMatch, Toast.LENGTH_LONG).show();
} */
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.END)) {
drawer.closeDrawer(GravityCompat.END);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
//noinspection SimplifiableIfStatement
if (id == R.id.menuRight) {
if (drawer.isDrawerOpen(Gravity.RIGHT)) {
drawer.closeDrawer(Gravity.RIGHT);
} else {
drawer.openDrawer(Gravity.RIGHT);
}
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.Home_page) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
} else if (id == R.id.not_pay) {
if (SmsInbox.this.drawer != null && SmsInbox.this.drawer.isDrawerOpen(GravityCompat.END)) {
SmsInbox.this.drawer.closeDrawer(GravityCompat.END);
} else {
Intent intent = new Intent(this, MainActivity.class);
SmsInbox.this.startActivity(intent);
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
}
} else if (id == R.id.date_pay) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
} else if (id == R.id.bill_sms) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
} else if (id == R.id.help_menu) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
} else if (id == R.id.for_us) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
} else if (id == R.id.exit_app) {
finish();
overridePendingTransition(0, 0);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.END);
return true;
}
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
答案 0 :(得分:0)
你应该使用正则表达式从字符串中扩展数字
Pattern p = Pattern.compile("-?\\d+");
Matcher m = p.matcher("There are more than -2 and less than 12 numbers here");
while (m.find()) {
System.out.println(m.group());
}
在视图点击监听器上,您可以执行此操作。
@Override
void onClick(View view){
String allTxt = editText.getText.toString()
Pattern p = Pattern.compile("-?\\d+");
Matcher m = p.matcher(allTxt);
while (m.find()) {
System.out.println(m.group());//here are the numbers then you can set it to another TextView
}
}
答案 1 :(得分:0)
String str = "test-asdfdfg 455 yuoyr 4";
str = str.replaceAll("[^-?0-9]+", " ");
System.out.println(Arrays.asList(str.trim().split(" ")));
输出[455,4]使用此数组显示选择器