Android两种按钮点击事件触发方式 和 一些简单的布局

第一种方式

------------MainActivity
    public void click(View v){
        Log.i("让我看看!!","button is 点击.");
    }
	
	    public void click1(View v){
			System.out.println("Hello!");
			AlertDialog dlog = new AlertDialog.Builder(this)
					.setTitle("对话框")
					.setIcon(R.mipmap.ic_launcher)
					.setMessage("要保存文件吗?")
					.setPositiveButton("ok",null)
					.setNegativeButton("no",null)
					.create();
				dlog.show();
		}
----------xml-------------
<?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"
    tools:context=".MainActivity"
    android:orientation="vertical">


    <ImageView
        android:layout_width="match_parent"
        android:layout_height="253dp"
        android:src="@mipmap" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="账号:"
            android:textSize="30sp"
            />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密码:"
            android:textSize="30sp"
            />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="click"
        android:text="sin"
         />
</LinearLayout>

在页面内动态监听按钮点击,而不是使用组件的onClick属性

package org.magicBox.myapplication;

import androidx.appcompat.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 btn_GetTo = (Button)findViewById(R.id.btn_GetTo);
        btn_GetTo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view){
                Toast.makeText(MainActivity.this,"跳转第二界面",Toast.LENGTH_LONG).show();
            }
        });
    }


}


配置最先加载的页面
```xml
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
posted @ 2022-03-14 20:16  白芷弋  阅读(590)  评论(0编辑  收藏  举报