文章目录
前言
一、Android Studio中新建一个JAVA项目
二、全部代码
1.activity_main.xml
2.activity_welcome.xml
3.MainActivity.java
4.WelcomeActivity
5.图片
前言
在开始之前信赖大家应该都安装好Android Studio这款软件了,本次就跟大家分享一个简单的Android Studio页面。
一、Android Studio中新建一个JAVA项目
首先进入软件,新建一个项目:File——> New ——> New project.
选择Empty Views Actvity,点击Next.
新建项目名为test2,语言为JAVA ,其他选项都默认,之后点击finish完成创建。
二、全部代码
1.运行结果
1.activity_main.xml
页面布局 res——>layout ——>activity_main.xml
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@drawable/img">
- <!-- 其他组件内容保持不变 -->
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="用户名:"
- android:layout_marginTop="50dp"
- android:layout_marginLeft="50dp"
- />
- <EditText
- android:id="@+id/usernameEditText"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginLeft="100dp"/>
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="50dp"
- android:text="密码:" />
- <EditText
- android:id="@+id/passwordEditText"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginLeft="100dp"
- android:inputType="textPassword" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="50dp"
- android:text="年龄:" />
- <Spinner
- android:id="@+id/ageSpinner"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginLeft="200dp"/>
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="50dp"
- android:text="性别:" />
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal">
- <RadioButton
- android:id="@+id/maleRadioButton"
- android:layout_width="wrap_content"
- android:layout_marginEnd="150dp"
- android:layout_marginStart="140dp"
- android:layout_height="wrap_content"
- android:text="男" />
- <RadioButton
- android:id="@+id/femaleRadioButton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="-75dp"
- android:text="女" />
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:orientation="horizontal">
- <Button
- android:id="@+id/loginButton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginEnd="150px"
- android:text="登陆" />
- <Button
- android:id="@+id/cancelButton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="取消" />
- </LinearLayout>
- </LinearLayout>
复制代码
2.activity_welcome.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@drawable/img">
- <TextView
- android:id="@+id/welcomeTextView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="100dp"
- android:layout_marginLeft="150dp"
- android:textSize="50dp"
- android:text="欢迎!" />
- <TextView
- android:id="@+id/welcomeUsernameTextView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="35dp"/>
- <TextView
- android:id="@+id/welcomePasswordTextView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="35dp"/>
- </LinearLayout>
复制代码 3.MainActivity.java
- package com.example.test2;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.ArrayAdapter;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.RadioButton;
- import android.widget.Spinner;
- import android.widget.Toast;
- import androidx.appcompat.app.AppCompatActivity;
- public class MainActivity extends AppCompatActivity {
- private EditText usernameEditText;
- private EditText passwordEditText;
- private Spinner ageSpinner;
- private RadioButton maleRadioButton;
- private RadioButton femaleRadioButton;
- private Button loginButton;
- private Button cancelButton;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- usernameEditText = findViewById(R.id.usernameEditText);
- passwordEditText = findViewById(R.id.passwordEditText);
- ageSpinner = findViewById(R.id.ageSpinner);
- maleRadioButton = findViewById(R.id.maleRadioButton);
- femaleRadioButton = findViewById(R.id.femaleRadioButton);
- loginButton = findViewById(R.id.loginButton);
- cancelButton = findViewById(R.id.cancelButton);
- // 设置年龄选择器的选项
- ArrayAdapter<CharSequence> ageAdapter = ArrayAdapter.createFromResource(this,
- R.array.age_options, android.R.layout.simple_spinner_item);
- ageAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
- ageSpinner.setAdapter(ageAdapter);
- loginButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- String username = usernameEditText.getText().toString();
- String password = passwordEditText.getText().toString();
- String selectedAge = ageSpinner.getSelectedItem().toString();
- String gender = maleRadioButton.isChecked()? "男" : "女";
- if (!username.isEmpty() &&!password.isEmpty()) {
- // 跳转到欢迎页面,并传递账号和密码
- Intent intent = new Intent(MainActivity.this, WelcomeActivity.class);
- intent.putExtra("username", username);
- intent.putExtra("password", password);
- startActivity(intent);
- } else {
- Toast.makeText(MainActivity.this, "请输入用户名和密码", Toast.LENGTH_SHORT).show();
- }
- }
- });
- cancelButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // 取消操作可以清空输入框等
- usernameEditText.setText("");
- passwordEditText.setText("");
- ageSpinner.setSelection(0);
- maleRadioButton.setChecked(false);
- femaleRadioButton.setChecked(false);
- }
- });
- }
- }
复制代码 4.WelcomeActivity
- package com.example.test2;
- import android.os.Bundle;
- import android.widget.TextView;
- import androidx.appcompat.app.AppCompatActivity;
- public class WelcomeActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_welcome);
- TextView welcomeUsernameTextView = findViewById(R.id.welcomeUsernameTextView);
- TextView welcomePasswordTextView = findViewById(R.id.welcomePasswordTextView);
- String username = getIntent().getStringExtra("username");
- String password = getIntent().getStringExtra("password");
- welcomeUsernameTextView.setText("用户名:" + username);
- welcomePasswordTextView.setText("密码:" + password);
- }
- }
复制代码 5.图片
图片都是放在drawable文件中,可以从外部复制粘贴到该文件夹中。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |