Android Studio 浅易登陆页面制作-JAVA
文章目录前言
一、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.
https://i-blog.csdnimg.cn/direct/e1e7cdebf94841aea5a93a638705a738.png
选择Empty Views Actvity,点击Next.
https://i-blog.csdnimg.cn/direct/23ff6a5732c74a98ab74f25af6d7c0f9.png
新建项目名为test2,语言为JAVA ,其他选项都默认,之后点击finish完成创建。
https://i-blog.csdnimg.cn/direct/282c1a03700b467a9d7fbc77a2610879.png
二、全部代码
1.运行结果
https://i-blog.csdnimg.cn/direct/812515c978114cafb3f2048a6866b5ba.png https://i-blog.csdnimg.cn/direct/dce0a0d4fb4b4d4bbf1d5f01c77c25a3.png
1.activity_main.xml
页面布局 res——>layout ——>activity_main.xml
https://i-blog.csdnimg.cn/direct/1e7db8ca6a1c48e087d4e125c41b6ebd.png
<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
https://i-blog.csdnimg.cn/direct/1e7db8ca6a1c48e087d4e125c41b6ebd.png
<?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
https://i-blog.csdnimg.cn/direct/2e98c082fb2d4f628e150f0b71e3ce5e.png
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.图片
https://i-blog.csdnimg.cn/direct/0d254f367a664a049407696eee3ac914.png
图片都是放在drawable文件中,可以从外部复制粘贴到该文件夹中。
https://i-blog.csdnimg.cn/direct/6355de7366f14bc189817a41e45f29d0.png
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]