Skip to content

Commit

Permalink
Merge pull request #1177 from Eheech/master
Browse files Browse the repository at this point in the history
#5 提交实验五代码
  • Loading branch information
zengsn authored Apr 2, 2019
2 parents 937f6b7 + 8f15e66 commit 3c7f6ef
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 8 deletions.
64 changes: 64 additions & 0 deletions students/soft1714080902421/SettingActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,76 @@

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.Toast;

import java.io.FileInputStream;
import java.io.FileOutputStream;


public class SettingActivity extends AppCompatActivity {
private EditText et_info;
private Button mBtnread=null;
private Button mBtnsave=null;


@Override

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
//获取布局文件中的控件
et_info = findViewById(R.id.et_info);
mBtnread = findViewById(R.id.btn_read);
mBtnsave = findViewById(R.id.btn_save);
setOnClickListener();
//根据id找到按钮
}

private void setOnClickListener(){
OnClick onClick = new OnClick();
mBtnread.setOnClickListener(onClick);
mBtnsave.setOnClickListener(onClick);
//设置点击事件
}

private class OnClick implements View.OnClickListener{
@Override
public void onClick(View v){
switch (v.getId()) {
case R.id.btn_save:
String saveinfo=et_info.getText().toString().trim();
FileOutputStream fos;
try {
//保存设定
fos = openFileOutput("data.txt", MODE_APPEND);
fos.write(saveinfo.getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
Toast.makeText(SettingActivity.this, "设定保存成功", Toast.LENGTH_SHORT).show();
break;
case R.id.btn_read:
String context = "";
try {
//获取已保存设定
FileInputStream fis = openFileInput("data.txt");
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
context = new String(buffer);
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
Toast.makeText(SettingActivity.this, "已保存的设定为:" + context,Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}

}
46 changes: 38 additions & 8 deletions students/soft1714080902421/activity_setting.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,44 @@
android:layout_height="match_parent"
tools:context="edu.hzuapps.androidlabs.soft1714080902421.SettingActivity">

<ImageView
android:id="@+id/iv_1"
android:layout_width="match_parent"
android:layout_height="match_parent"
<TextView
android:id="@+id/setting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:background="#f99999"
android:scaleType="fitXY"
android:src="@drawable/setting" />
android:textSize="20dp"
android:text="请输入种树成功回复:"/>

<EditText
android:id="@+id/et_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/setting"
android:layout_alignParentLeft="true"
android:layout_marginLeft="79dp"
android:layout_marginTop="16dp"
android:ems="10" />
<requestFocus/>

<Button
android:id="@+id/btn_read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/et_info"
android:layout_alignParentRight="@id/et_info"
android:layout_marginLeft="79dp"
android:layout_marginTop="18dp"
android:text="读取设定" />

<Button
android:id="@+id/btn_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/et_info"
android:layout_alignParentLeft="true"
android:layout_marginLeft="191dp"
android:layout_marginTop="18dp"
android:text="保存设定" />

</RelativeLayout>

0 comments on commit 3c7f6ef

Please sign in to comment.