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>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通