建立一个app,要求输入用户名,年龄,性别,通信地址,然后单击注册将输入信息保存至文件中,使用android
https://blog.csdn.net/qq_42876285/article/details/111252107
对于很多已经从事Android开发的朋友,如何学习提升,如何制定一个正确体系化的学习路线尤为重要,这里分享一下我的经验;
问题解答:
首先要创建一个登录页面,其中包括用户名、年龄、性别和通讯地址的输入框,以及登录和注册按钮。
布局文件login_activity.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="用户名" />
<EditText
android:id="@+id/et_age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="年龄" />
<Spinner
android:id="@+id/spin_gender"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/gender_array"
android:prompt="@string/gender_prompt" />
<EditText
android:id="@+id/et_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="通讯地址" />
<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录" />
<Button
android:id="@+id/btn_register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注册" />
</LinearLayout>
其中,Spinner是选择性别的下拉框,@array/gender_array为其中的选项,@string/gender_prompt为提示文字。
接下来,要在MainActivity中处理登录和注册按钮的点击事件,并将输入的信息保存到文件中。
MainActivity的代码如下:
public class MainActivity extends AppCompatActivity {
private EditText etUsername;
private EditText etAge;
private Spinner spinGender;
private EditText etAddress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_activity);
etUsername = findViewById(R.id.et_username);
etAge = findViewById(R.id.et_age);
spinGender = findViewById(R.id.spin_gender);
etAddress = findViewById(R.id.et_address);
Button btnLogin = findViewById(R.id.btn_login);
Button btnRegister = findViewById(R.id.btn_register);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//处理登录逻辑
}
});
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//处理注册逻辑
String username = etUsername.getText().toString();
String age = etAge.getText().toString();
String gender = spinGender.getSelectedItem().toString();
String address = etAddress.getText().toString();
//将用户输入的信息保存到文件中
try {
FileOutputStream fos = openFileOutput("user_info.txt", Context.MODE_PRIVATE);
OutputStreamWriter osw = new OutputStreamWriter(fos);
osw.write(username + "\n" + age + "\n" + gender + "\n" + address + "\n");
osw.close();
Toast.makeText(MainActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
其中,将用户输入的信息保存到文件中的部分代码如下:
FileOutputStream fos = openFileOutput("user_info.txt", Context.MODE_PRIVATE);
OutputStreamWriter osw = new OutputStreamWriter(fos);
osw.write(username + "\n" + age + "\n" + gender + "\n" + address + "\n");
osw.close();
以上是一个简单的登录界面应用的实现方法。