android的按钮(Button)的实例解析
程序员文章站
2022-06-27 18:40:18
运行效果在xml添加代码
运行效果
在xml添加代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#EFEFF4"
android:orientation="vertical"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="604dp"
android:scaleType="fitStart"
android:src="@drawable/login2" />
<Button
android:id="@+id/login"
android:layout_width="match_parent"
android:layout_height="61dp"
android:background="#2196F3"
android:text="授权并登录"
android:textColor="#FFFFFF"
android:textSize="20dp" />
</LinearLayout>
在Java文件添加代码
package com.example.wangzhelogin2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button login=(Button)findViewById(R.id.login);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,"你已授权登录王者荣耀",Toast.LENGTH_LONG).show();
}
});
}
}
本文地址:https://blog.csdn.net/qq_44716544/article/details/107898803