代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tianjia);
Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar1);
toolbar.setTitle("添加");
toolbar.inflateMenu(R.menu.menu2);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
Intent intent=new Intent();
Bundle bundle=new Bundle();
name=(EditText)findViewById(R.id.name);
String name1=name.getText().toString();
phone=(EditText)findViewById(R.id.phone);
String phone1=phone.getText().toString();
FileInputStream in;
BufferedReader read = null;
String s1="",s2="";
try {
in = openFileInput("date5");
read = new BufferedReader(new InputStreamReader(in));
String line = "";
while ((line = read.readLine()) != null) {
StringTokenizer st = new StringTokenizer(line, ";"); //设置数据之间 的分隔符为;
s1 = st.nextToken();//读取的时候是 String 类
s2 = st.nextToken();
if (s1== name1 || s2 == phone1 ) {
Toast.makeText(tianjia.this, "该快捷拨号已存在", Toast.LENGTH_SHORT).show();
//AlertDialog alertDialog=new AlertDialog();
break;
}
}
}catch (IOException e){
e.printStackTrace();
}finally {
try {
if (read != null) {
read.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
if(s1!= name1&&s2 != phone1&&!TextUtils.isEmpty(name1)&&!TextUtils.isEmpty(phone1))
{
bundle.putString("name1",name1);
bundle.putString("phone1",phone1);
intent.putExtras(bundle);
setResult(RESULT_OK,intent);
finish();
}
return false;
}
});
}
@Override
protected void onDestroy() {
if(name!=null) {
String Input1 = name.getText().toString();
String Input2 = phone.getText().toString();
if(!TextUtils.isEmpty(Input1)&&!TextUtils.isEmpty(Input2)) save(Input1, Input2);
}
super.onDestroy();
}
public void save(String i1,String i2){
FileOutputStream out;
BufferedWriter writer=null;
FileInputStream in;
BufferedReader read=null;
String s1=null,s2=null;
try{
out=openFileOutput("date5", Context.MODE_APPEND);
in=openFileInput("date5");
writer=new BufferedWriter(new OutputStreamWriter(out));
read = new BufferedReader(new InputStreamReader(in));
String line="";
while((line=read.readLine())!=null){
StringTokenizer st = new StringTokenizer(line, ";"); //设置数据之间 的分隔符为;
s1 = st.nextToken();//读取的时候是 String 类
s2 = st.nextToken();
if(s1==i1||s2==i2) {
Toast.makeText(tianjia.this, "该快捷拨号已存在", Toast.LENGTH_SHORT).show();
return;
}
}
if(s1!=i1&&s2!=i2&&!TextUtils.isEmpty(i1)&&!TextUtils.isEmpty(i2)) {
writer.write(i1);
writer.write(";");
writer.write(i2);
writer.write(";");
writer.newLine();
}
}catch(IOException e){
e.printStackTrace();
}finally {
try {
if (writer != null) {
writer.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
}
Toast标签一直没显示,说明判断是否相等处错了,但不知道为什么,求解!!!!!