Android Java开发学习 Day2


教学视频源为BilibiliBV1Np4y1a7UN

控件的添加

添加控件有一个流程,可以概括为:
①在res文件夹中对控件进行注册
②在java目录下Create控件
③重写关于控件的事件函数
以以下两个控件为例:
(ps:不过这是我今天学习的体会,可能有些不对。)

在注册控件时一定要定义控件的ID,其他的元素按照需求添加

一、Button控件

①在res文件夹中注册控件:
进入到res->layout,进入之前建立的.xml文件,注册Button:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<!--对Button进行注册 -->
    <Button
        android:id="@+id/Button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="80dp"
        android:text="Button_1"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_editor_absoluteX="0dp" />
<!--注册Button结束 -->
</androidx.constraintlayout.widget.ConstraintLayout>

②Create控件,注意函数名不能写错

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.first_layout);//在layout目录下定义的layout,可以用这个函数加载在视图上

        Button button1 = (Button) findViewById(R.id.Button1);//通过ID找到元素,然后类型转化为按钮
        button1.setOnClickListener(new View.OnClickListener() {
            //对button进行侦听
            //传入参数应该是一个View. View.OnClickListener()方法,这是一种类似C++lambda的创建方式
            @Override
            public void onClick(View v) {
                Toast.makeText(MyFirstActivity.this,"First app",Toast.LENGTH_LONG).show();
            }//第一个参数是上下文,也就是说是在MyFirstActivity这个content是上下文
        });
    }

③重写关于控件事件的函数:以上面代码为例就是onClick,写在button的侦听函数内。
其中Toast是在底部出现的小弹窗,调用Toast.makeText可以显示内容。

二、Menu控件

①与上面一样,首先在res中注册控件:新建menu文件夹,新建main.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/add_item"
        android:title="添加"/>
    <item android:id="@+id/remove_item"
        android:title="删除"/>
</menu>

②Create控件

    public boolean onCreateOptionsMenu(Menu menu){
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

③重写事件函数:

@Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item){
        if(item.getItemId() == R.id.add_item) {
            Toast.makeText(this, "添加", Toast.LENGTH_SHORT).show();
        }
        else if(item.getItemId() == R.id.remove_item){
            Toast.makeText(this,"删除",Toast.LENGTH_SHORT).show();
        }
        return true;
    }

三、Intent调用Activity

从一个Activity调用另一个Activity需要用到intent,过程如下:
①右键java文件夹下的com.example.项目名文件夹新建一个Activity,同时也会生成一个.xml文件。
②在AndroidManifest.xml中进行编辑修改,注意要把exported字段设置为true,这样才能被其他的activity调用。
③在调用该新增Activity的Activity内新增intent对象。
调用方法分为显式调用与隐式调用,如下:

显式调用

在AndroidManifest.xml中定义该activity

 		<activity
            android:name=".SecondActivity"
            android:exported="true">
        </activity>

调用方式:

 			@Override
            public void onClick(View v) {
                Intent intent = new Intent(MyFirstActivity.this, SecondActivity.class);
                startActivity(intent);
            }//第一个参数是上下文,也就是说是在MyFirstActivity这个content是上下文

Intent的构造函数第一个参数表示调用该活动的类,第二个表示调用哪一个活动

隐式调用

需要添加intent-filter
action android:name是自己定义的标识符

 		<activity
            android:name=".SecondActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="com.example.activitytest.ACTION_START"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="com.example.activitytest.MY_CATEGORY"/>
            </intent-filter>
        </activity>

调用方式:

 @Override
            public void onClick(View v) {
                Intent intent = new Intent("com.example.activitytest.ACTION_START");
                //intent.addCategory("com.example.activitytest.MY_CATEGORY");
                startActivity(intent);
            }//第一个参数是上下文,也就是说是在MyFirstActivity这个content是上下文

通过在AndroidManifest中定义的标识符来启动,addCategory进一步限定 Intent 的匹配条件,确保只有声明了对应 Category 的 Activity 能响应。注意要addCategory,android.intent.category.DEFAULT是默认的category,也就是说如果不指定Category就直接从default category掉用,如果指定就需要addCategory。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值