<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/abd"
android:text="hello!"
android:layout_centerInParent="true"
/>
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// getSupportActionBar().hide();
Button abc= findViewById(R.id.abd);
abc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
startActivity(intent);
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".MainActivity2"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="afcugdysvdskisdh"
android:textSize="50dp"
android:textAlignment="center"
android:layout_marginTop="50dp"
/>
<TextView
android:id="@+id/d2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="afcugdysvdskisdh"
android:textSize="50dp"
android:textAlignment="center"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/b3"
android:text="change"
android:textSize="25dp"
/>
</LinearLayout>
public class MainActivity2 extends AppCompatActivity {
boolean isChanged=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
TextView t2=findViewById(R.id.d2);
Button b2=findViewById(R.id.b3);
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!isChanged){
Date now=new Date();
SimpleDateFormat s=new SimpleDateFormat("yyyy-年-MM-月-dd-日");
String ss=s.format(now);
t2.setText(ss);
isChanged=true;
}
else {
t2.setText("afcugdysvdskisdh");
isChanged=false;
}
}
});
}
}
File files=getExternalFilesDir("");
File a=new File(files.getAbsolutePath()+"/time");
try {
a.createNewFile();
} catch (IOException e) {
throw new RuntimeException(e);
}
try {
BufferedWriter w=new BufferedWriter(new FileWriter(a));
Date now=new Date();
SimpleDateFormat s=new SimpleDateFormat("yyyy-年-MM-月-dd-日");
String ss=s.format(now);
w.write(ss);
w.flush();
w.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
<style name="Theme.Practice" parent="Theme.AppCompat.Light.NoActionBar">
(1)主页面? ? ? ? ? ? ? ? ? ?(2)点击按钮进入此页面(3)点击按钮实现文段替换
(4)文件写入查看