forked from hzuapps/android-labs-2018
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
Soft1614080902114/app/src/main/java/edu/hzuapps/android/soft1614080902114/Main4Activity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.example.soft1614080902114; | ||
|
||
import android.support.v7.app.AppCompatActivity; | ||
import android.os.Bundle; | ||
import android.view.View; | ||
import android.widget.Button; | ||
import android.widget.EditText; | ||
import android.widget.TextView; | ||
|
||
import java.io.FileInputStream; | ||
import java.io.FileOutputStream; | ||
|
||
public class Main4Activity extends AppCompatActivity { | ||
|
||
private Button seek; | ||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_main4); | ||
seek=(Button)findViewById(R.id.button_seek); | ||
seek.setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View v) { | ||
final EditText tx= (EditText) findViewById(R.id.edittext_input); | ||
String s = tx.getText().toString(); | ||
if(!s.isEmpty()) | ||
save(s); | ||
refresh(s); | ||
} | ||
}); | ||
init(); | ||
} | ||
|
||
public void save(String msg){ | ||
if(msg == null) return; | ||
try { | ||
msg += read(); | ||
FileOutputStream fos = openFileOutput("storage.txt", | ||
MODE_APPEND); | ||
fos.write(msg.getBytes()); | ||
fos.close(); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public String read() { | ||
try { | ||
FileInputStream inStream = this.openFileInput("message.txt"); | ||
byte[] buffer = new byte[1024]; | ||
int hasRead = 0; | ||
StringBuilder sb = new StringBuilder(); | ||
while ((hasRead = inStream.read(buffer)) != -1) { | ||
sb.append(new String(buffer, 0, hasRead)); | ||
} | ||
inStream.close(); | ||
return sb.toString(); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
return null; | ||
} | ||
|
||
private void refresh(String msg) | ||
{ | ||
if(msg==null) return; | ||
TextView textView = (TextView) findViewById(R.id.textview_history); | ||
String s = textView.getText().toString(); | ||
textView.setText(s+"\n"+msg); | ||
} | ||
|
||
private void init() | ||
{ | ||
TextView textView = (TextView) findViewById(R.id.textview_history); | ||
String s = read(); | ||
if(s!=null) | ||
refresh(s); | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
Soft1614080902114/app/src/main/res/layout/activity_main4.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
tools:context=".Main4Activity"> | ||
|
||
|
||
<LinearLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:orientation="vertical"> | ||
|
||
<LinearLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="60dp" | ||
android:orientation="horizontal"> | ||
|
||
<EditText | ||
android:id="@+id/edittext_input" | ||
android:layout_width="0dp" | ||
android:layout_height="match_parent" | ||
android:layout_weight="5" | ||
android:hint="输入" /> | ||
<Button | ||
android:id="@+id/button_seek" | ||
android:layout_width="80dp" | ||
android:layout_height="match_parent" | ||
android:text="查找" | ||
android:layout_gravity="center_horizontal"/> | ||
|
||
</LinearLayout> | ||
|
||
|
||
<TextView | ||
android:id="@+id/textview_history" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:text="查询历史:" | ||
/> | ||
</LinearLayout> | ||
</android.support.constraint.ConstraintLayout> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.