实现换行
replace和replaceAll的区别
1)replace的参数是char和CharSequence,即可以支持字符的替换,也支持字符串的替换(CharSequence即字符串序列的意思,说白了也就是字符串);
2)replaceAll的参数是regex,即基于规则表达式的替换,比如,可以通过replaceAll("\\d", "*")把一个字符串所有的数字字符都换成星号;
相同点是都是全部替换,即把源字符串中的某一字符或字符串全部换成指定的字符或字符串,如果只想替换
第一次
出现的,可以使用 replaceFirst(),这个方法也是基于规则表达式的替换,但与replaceAll()不同的是,只替换
第一次
出现的字符串;
另外,如果replaceAll()和replaceFirst()所用的参数据不是基于规则表达式的,则与replace()替换字符串的效果是一样的,即这两者也支持字符串的操作;
还有一点注意:执行了替换操作后,源字符串的内容是没有发生改变的.
举例如下:
String src = new String("ab43a2c43d");
System.out.println(src.replace("3","f"));=>ab4f2c4fd.
System.out.println(src.replace('3','f'));=>ab4f2c4fd.
System.out.println(src.replaceAll("\\d","f"));=>abffafcffd.
System.out.println(src.replaceAll("a","f"));=>fb43fc23d.
System.out.println(src.replaceFirst("\\d,"f"));=>abf32c43d
System.out.println(src.replaceFirst("4","h"));=>abh32c43d.
package webb.wechat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private RadioGroup rgp;
private LinearLayout linear1, linear2, linear3, linear4;
//linear1
private RadioGroup linear1_rgp;
private Button btn_linear_submit;
//linear2
private LinearLayout linear2_cb;
private CheckBox linear2_cb_all;
private Button linear2_btn_del;
//linear3
private Spinner spinner_sum;
private Spinner spinner_sum_country;
private RelativeLayout linear_countries;
private static final String TEXT_KEY = "name";
private static final String IMAGE_KEY = "img";
//linear4
public List personList = new ArrayList<>();
private TextView linear4_tv;
private Spinner nameOrlike;
private AutoCompleteTextView auto_check;
//国家集合
public List> list = new ArrayList<>();
//城市集合
public List> city_list_China = new ArrayList<>();
//空集合
public List> city_list_null = new ArrayList<>();
//nameOrlike
public String[] nameOrlikes;
public String[] auto_name;
public String[] auto_like;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//添加数据
addData();
//关联
initView();
//点击事件
initClick();
//绑定
initApp();
}
private void initView() {
//实例化
rgp = (RadioGroup) findViewById(R.id.rdg);
linear1 = (LinearLayout) findViewById(R.id.linear1);
linear1_rgp = (RadioGroup) findViewById(R.id.linear1_rgp);
btn_linear_submit = (Button) findViewById(R.id.btn_OnSubmit);
linear2 = (LinearLayout) findViewById(R.id.linear2);
linear2_cb = (LinearLayout) findViewById(R.id.linear2_cb);
linear2_cb_all = (CheckBox) findViewById(R.id.linear2_cb_all);
linear2_btn_del = (Button) findViewById(R.id.linear2_btn_del);
linear2_cb_all.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean ischecked) {
for (int i = 0; i < linear2_cb.getChildCount(); i++) {
CheckBox cb = (CheckBox) linear2_cb.getChildAt(i);
if (ischecked) {
linear2_btn_del.setVisibility(View.VISIBLE);
cb.setChecked(ischecked);
} else {
linear2_btn_del.setVisibility(View.GONE);
cb.setChecked(ischecked);
}
}
}
});
linear3 = (LinearLayout) findViewById(R.id.linear3);
spinner_sum = (Spinner) findViewById(R.id.spinner_sum);
spinner_sum_country = (Spinner) findViewById(R.id.spinner_sum_country);
linear_countries = (RelativeLayout) findViewById(R.id.linear_countries);
linear4 = (LinearLayout) findViewById(R.id.linear4);
linear4_tv = (TextView) findViewById(R.id.linear4_tv);
nameOrlike = (Spinner) findViewById(R.id.nameOrlike);
auto_check = (AutoCompleteTextView) findViewById(R.id.auto_check);
}
/**
* 添加数据
*/
private void addData() {
//添加国家
HashMap hashMap = new HashMap<>();
hashMap.put(TEXT_KEY, "Afghanistan");
hashMap.put(IMAGE_KEY, R.mipmap.daxiao);
list.add(hashMap);
hashMap = new HashMap<>();
hashMap.put(TEXT_KEY, "Algeria");
hashMap.put(IMAGE_KEY, R.mipmap.fennu);
list.add(hashMap);
hashMap = new HashMap<>();
hashMap.put(TEXT_KEY, "Belarus");
hashMap.put(IMAGE_KEY, R.mipmap.ic_launcher);
list.add(hashMap);
hashMap = new HashMap<>();
hashMap.put(TEXT_KEY, "Ecuador");
hashMap.put(IMAGE_KEY, R.mipmap.shangxin);
list.add(hashMap);
hashMap = new HashMap<>();
hashMap.put(TEXT_KEY, "Ethiopia");
hashMap.put(IMAGE_KEY, R.mipmap.shuaku);
list.add(hashMap);
hashMap = new HashMap<>();
hashMap.put(TEXT_KEY, "France");
hashMap.put(IMAGE_KEY, R.mipmap.tiaopi);
list.add(hashMap);
hashMap = new HashMap<>();
hashMap.put(TEXT_KEY, "China");
hashMap.put(IMAGE_KEY, R.mipmap.weixiao);
list.add(hashMap);
//添加城市China
HashMap city_China = new HashMap<>();
city_China.put(TEXT_KEY, "朝阳区");
city_China.put(IMAGE_KEY, R.mipmap.mei);
city_list_China.add(city_China);
city_China = new HashMap<>();
city_China.put(TEXT_KEY, "海淀区");
city_China.put(IMAGE_KEY, R.mipmap.you);
city_list_China.add(city_China);
city_China = new HashMap<>();
city_China.put(TEXT_KEY, "顺义区");
city_China.put(IMAGE_KEY, R.mipmap.tu);
city_list_China.add(city_China);
city_China = new HashMap<>();
city_China.put(TEXT_KEY, "昌平区");
city_China.put(IMAGE_KEY, R.mipmap.le);
city_list_China.add(city_China);
city_China = new HashMap<>();
city_China.put(TEXT_KEY, "通州区");
city_China.put(IMAGE_KEY, R.mipmap.meile);
city_list_China.add(city_China);
//添加城市Algeria
HashMap city_Algeria = new HashMap<>();
//添加城市Ecuador
HashMap city_Ecuador = new HashMap<>();
/**
* Linear4
* 添加数据
*/
Person p1 = new Person();
p1.name = "王伟旭";
p1.like = "篮球";
p1.age = "18";
p1.sex = "男";
p1.blood = "A型血";
personList.add(p1);
Person p2 = new Person();
p2.name = "王伟旭";
p2.like = "羽毛球";
p2.age = "18";
p2.sex = "男";
p2.blood = "A型血";
personList.add(p2);
Person p3 = new Person();
p3.name = "王伟旭";
p3.like = "羽毛球";
p3.age = "118";
p3.sex = "男";
p3.blood = "P型血";
personList.add(p3);
//添加爱好/姓名
nameOrlikes = getResources().getStringArray(R.array.nameOrlike);
auto_name = getResources().getStringArray(R.array.auto_name);
auto_like = getResources().getStringArray(R.array.auto_like);
}
/**
* 绑定国家
*/
private void initApp() {
SimpleAdapter adapter_countries = new SimpleAdapter(
this,
list,
R.layout.countres_layout,
new String[]{IMAGE_KEY, TEXT_KEY},
new int[]{R.id.iv_country_img, R.id.tv_country_name}
);
spinner_sum.setAdapter(adapter_countries);
//绑定城市
//绑定适配器爱好名字
ArrayAdapter nameadapter = new ArrayAdapter(
this,
R.layout.support_simple_spinner_dropdown_item,
nameOrlikes
);
nameOrlike.setAdapter(nameadapter);
}
private void initClick() {
/**
* linear4
* 选择爱好/姓名
*/
nameOrlike.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView> parent, View view, int position, long id) {
if (position == 0) {
auto_check.setText("");
Toast.makeText(MainActivity.this, "姓名", Toast.LENGTH_SHORT).show();
add_adapter_name();
//显示详细信息
auto_check.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
String n = "";
@Override
public void afterTextChanged(Editable s) {
for (Person p : personList) {
if (auto_check.getText().toString().equals(p.name)) {
// Toast.makeText(MainActivity.this, "王伟旭", Toast.LENGTH_SHORT).show();
linear4_tv.setText("");
StringBuffer sb = new StringBuffer();
sb.append("姓名:" + p.name + "爱好:" + p.like + "年龄:" + p.age + "血型:" + p.blood + ";" + "\n");
Toast.makeText(MainActivity.this, sb, Toast.LENGTH_SHORT).show();
String name = sb.toString().replaceAll("\\n", "\n");
n = n + sb;
linear4_tv.setText(n);
} else {
linear4_tv.setText("");
}
}
}
});
} else {
auto_check.setText("");
Toast.makeText(MainActivity.this, "爱好", Toast.LENGTH_SHORT).show();
add_adapter_like();
//显示详细信息
auto_check.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
String n = "";
@Override
public void afterTextChanged(Editable s) {
for (Person p : personList) {
if (auto_check.getText().toString().equals(p.like)) {
// Toast.makeText(MainActivity.this, "王伟旭", Toast.LENGTH_SHORT).show();
linear4_tv.setText("");
StringBuffer sb = new StringBuffer();
sb.append("姓名:" + p.name + "爱好:" + p.like + "年龄:" + p.age + "血型:" + p.blood + ";" + "\n");
Toast.makeText(MainActivity.this, sb, Toast.LENGTH_SHORT).show();
String name = sb.toString().replaceAll("\\n", "\n");
n = n + sb;
linear4_tv.setText(n);
} else {
linear4_tv.setText("");
}
}
}
});
}
}
private void add_adapter_name() {
ArrayAdapter adpter = new ArrayAdapter(
MainActivity.this,
R.layout.support_simple_spinner_dropdown_item,
auto_name
);
auto_check.setAdapter(adpter);
}
private void add_adapter_like() {
ArrayAdapter adpter = new ArrayAdapter(
MainActivity.this,
R.layout.support_simple_spinner_dropdown_item,
auto_like
);
auto_check.setAdapter(adpter);
}
@Override
public void onNothingSelected(AdapterView> parent) {
}
});
//选择国家
spinner_sum.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView> parent, View view, int position, long id) {
LinearLayout countres = (LinearLayout) view;
TextView tv = (TextView) countres.getChildAt(1);
System.out.println("明晚:" + tv.getText().toString());
if (tv.getText().equals("China")) {
SimpleAdapter adapter_city = new SimpleAdapter(
MainActivity.this,
city_list_China,
R.layout.countres_layout,
new String[]{IMAGE_KEY, TEXT_KEY},
new int[]{R.id.iv_country_img, R.id.tv_country_name}
);
spinner_sum_country.setAdapter(adapter_city);
} else {
SimpleAdapter adapter_city_null = new SimpleAdapter(
MainActivity.this,
city_list_null,
R.layout.countres_layout,
new String[]{IMAGE_KEY, TEXT_KEY},
new int[]{R.id.iv_country_img, R.id.tv_country_name}
);
spinner_sum_country.setAdapter(adapter_city_null);
}
}
@Override
public void onNothingSelected(AdapterView> parent) {
}
});
rgp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int ischecked) {
for (int i = 0; i < rgp.getChildCount(); i++) {
RadioButton rdo = (RadioButton) rgp.getChildAt(i);
if (rdo.isChecked()) {
switch (rdo.getId()) {
case R.id.rdo_wechat:
linear1.setVisibility(View.VISIBLE);
linear2.setVisibility(View.GONE);
linear3.setVisibility(View.GONE);
linear4.setVisibility(View.GONE);
break;
case R.id.rdo_say:
linear2.setVisibility(View.VISIBLE);
linear1.setVisibility(View.GONE);
linear3.setVisibility(View.GONE);
linear4.setVisibility(View.GONE);
break;
case R.id.rdo_look:
linear3.setVisibility(View.VISIBLE);
linear2.setVisibility(View.GONE);
linear1.setVisibility(View.GONE);
linear4.setVisibility(View.GONE);
break;
case R.id.rdo_me:
linear4.setVisibility(View.VISIBLE);
linear2.setVisibility(View.GONE);
linear3.setVisibility(View.GONE);
linear1.setVisibility(View.GONE);
break;
}
}
}
}
});
linear1_rgp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int ischecked) {
for (int i = 0; i < linear1_rgp.getChildCount(); i++) {
RadioButton rdo = (RadioButton) linear1_rgp.getChildAt(i);
if (rdo.isChecked()) {
btn_linear_submit.setVisibility(View.VISIBLE);
}
}
}
});
}
//linear1
public void btn_OnSubmit(View view) {
for (int i = 0; i < linear1_rgp.getChildCount(); i++) {
RadioButton rdo = (RadioButton) linear1_rgp.getChildAt(i);
if (rdo.isChecked()) {
Toast.makeText(MainActivity.this, "哈哈哈" + rdo.getText().toString(), Toast.LENGTH_SHORT).show();
}
}
}
public void cb_del(View view) {
for (int i = 0; i < linear2_cb.getChildCount(); i++) {
CheckBox checkBox = (CheckBox) linear2_cb.getChildAt(i);
if (checkBox.isChecked()) {
checkBox.setVisibility(View.GONE);
}
}
}
}