赞
踩
目录
编译器选择:Android Studio
创建一个新的 Empty Activity,设置项目名称,选择项目路径及编程语言;点击 Finish 即可。
项目创建完成后,先将项目视图切换为Project模式
这里重点关注src组织下的内容:
下面我们将重心转回到activity_main.xml,学习线性布局,重点关注以以下几个要点:
我们可以在线性布局中定义相关的控件:
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/tv_username"
- android:hint="用户名"
- android:textColorHint="#FFFFFF"
- android:textColor="#FFFFFF"
- android:textSize="30sp"/>
必须定义的属性字段:
可选字段:
案例:在一个线性布局上,放置两个登录和注册按钮
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal"
- >
- <Button
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="登录"
- android:id="@+id/btn_login"
- />
- <Button
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="注册"
- android:id="@+id/btn_register"></Button>
- </LinearLayout>
自定义背景文件
登录按钮的背景文件定义代码:
- <?xml version="1.0" encoding="utf-8"?>
- <shape
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:shape="rectangle">
-
- <corners
- android:topLeftRadius="5dp"
- android:bottomLeftRadius="5dp"/>
- <stroke
- android:width="1dp"
- android:color="#ffffff"
- android:dashGap="1dp"/>
- </shape>
下面是完整的线性布局文件:
- <?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"
- android:orientation="vertical"
- android:background="@drawable/app_background"
- tools:context=".MainActivity">
-
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/tv_username"
- android:hint="用户名"
- android:textColorHint="#FFFFFF"
- android:textColor="#FFFFFF"
- android:textSize="30sp"
- android:padding="10dp"/>
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/tv_password"
- android:hint="密码"
- android:textColorHint="#FFFFFF"
- android:textColor="#FFFFFF"
- android:textSize="30sp"
- android:padding="10dp"
- android:inputType="textPassword"
- />
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal"
- >
- <Button
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="登录"
- android:textColor="#FFFFFF"
- android:id="@+id/btn_login"
- android:background="@drawable/bg_btnlogin"
- />
- <Button
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="注册"
- android:textColor="#FFFFFF"
- android:id="@+id/btn_register"
- android:background="@drawable/bg_btnregister"></Button>
- </LinearLayout>
- </LinearLayout>
效果如下:
方法1:在src -- main -- java下的MainActivity.java的同一层次下,创建一个新的Empty Activity,
设置完名字后,直接点击Finish。
可以看到,创建Activity的同时也创建了相应的布局文件。
同样地,我们在布局文件中,简单添加一个TextView,提示我们跳转成功了。
- <?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"
- android:orientation="vertical"
- android:padding="10dp"
- tools:context=".Main2Activity">
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/tv_tiaozhuan"
- android:text="跳转之后的网页"
- android:gravity="center"></TextView>
- </LinearLayout>
然后,在主Activity中的java文件中,添加跳转的代码:
- package com.example.myapplicationexample;
-
- import androidx.appcompat.app.AppCompatActivity;
-
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
-
- public class MainActivity extends AppCompatActivity {
- private Button mBtnLogin;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- // 根据id找到控件
- mBtnLogin = findViewById(R.id.btn_login);
- // 实现跳转
- mBtnLogin.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent intent = null;
- intent = new Intent(MainActivity.this, Main2Activity.class);
- startActivity(intent);
- }
- });
- }
- }
- public void onClick(View view) {
- String username = mEtUsername.getText().toString();
- String password = mEtPassword.getText().toString();
- Intent intent = null;
-
- if(username.equals("myz") && password.equals("gis2021"))
- {
- ToastUtil.showMessage(MainActivity.this, "账号密码正确");
- intent = new Intent(MainActivity.this, Main2Activity.class);
- startActivity(intent);
- }else {
-
- }
- }
- public class MainActivity extends AppCompatActivity implements View.OnClickListener {
- private Button mBtnLogin;
- private EditText mEtUsername;
- private EditText mEtPassword;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- // 根据id找到控件
- mBtnLogin = findViewById(R.id.btn_login);
- mEtPassword = findViewById(R.id.et_password);
- mEtUsername = findViewById(R.id.et_username);
- // alt + enter查看错误
- mBtnLogin.setOnClickListener(this);
- }
- public void onClick(View view) {
- String username = mEtUsername.getText().toString();
- String password = mEtPassword.getText().toString();
- Intent intent = null;
-
- if(username.equals("myz") && password.equals("gis2021"))
- {
- intent = new Intent(MainActivity.this, Main2Activity.class);
- startActivity(intent);
- }else {
- }
- }
-
- @Override
- public void onPointerCaptureChanged(boolean hasCapture) {
-
- }
- }
方法1:直接在底部显示:
Toast.makeText(getApplicationContext(), "账号密码正确", Toast.LENGTH_SHORT).show();
方法2:居中弹出:
- Toast centerToast = Toast.makeText(getApplicationContext(), "账号或密码错误", Toast.LENGTH_SHORT);
- centerToast.setGravity(Gravity.CENTER, 0,0);
- centerToast.show();
方法3:定义类弹出
- package com.example.myapplicationexample.util;
-
- import android.content.Context;
- import android.widget.Toast;
-
- public class ToastUtil {
- public static Toast mToast;
- public static void showMessage(Context context, String wsg)
- {
- if(mToast == null)
- {
- mToast = Toast.makeText(context, wsg, Toast.LENGTH_SHORT);
- }else{
- mToast.setText(wsg);
- }
- mToast.show();
- }
- }
ToastUtil.showMessage(MainActivity.this, "账号密码正确");
相关代码链接:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。