Android从入门到进阶

目录

helloworld

Activity

Menu


helloworld

java 复制代码
public class MainActivity2 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}
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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity2">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World"/>
</LinearLayout>

Activity

java 复制代码
public class MainActivity2 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button)findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity2.this,"点击了按钮",Toast.LENGTH_LONG).show();
            }
        });
    }
}
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:orientation="vertical"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity2">

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="按钮1"/>
</LinearLayout>
XML 复制代码
        <activity
            android:name=".MainActivity2"
            android:exported="true"
            android:label="按钮"
            android:theme="@style/Theme.AppCompat.Light">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
java 复制代码
public class MainActivity2 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button)findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity2.this,"点击了按钮",Toast.LENGTH_LONG).show();
               
            }
        });
    }

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

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        Log.i("MainActivity2",String.valueOf(item.getItemId()));
        Toast.makeText(MainActivity2.this,"点击按钮"+item.getTitle(),Toast.LENGTH_LONG).show();
        return super.onOptionsItemSelected(item);
    }
}
XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/add" android:title="添加"/>
    <item android:id="@+id/remove" android:title="删除"/>
</menu>
XML 复制代码
        <activity
            android:name=".MainActivity2"
            android:exported="true"
            android:label="按钮"
            android:theme="@style/Theme.AppCompat.Light">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

Intent

显示Intent

java 复制代码
public class MainActivity2 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button)findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Toast.makeText(MainActivity2.this,"点击了按钮",Toast.LENGTH_LONG).show();
                Intent intent = new Intent(MainActivity2.this,MainActivity3.class);
                startActivity(intent);
            }
        });
    }
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:orientation="vertical"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity2">

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="按钮1"/>
</LinearLayout>
XML 复制代码
        <activity
            android:name=".MainActivity2"
            android:exported="true"
            android:label="按钮"
            android:theme="@style/Theme.AppCompat.Light">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

隐示Intent

java 复制代码
public class MainActivity2 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button)findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Toast.makeText(MainActivity2.this,"点击了按钮",Toast.LENGTH_LONG).show();
//                Intent intent = new Intent(MainActivity2.this,MainActivity3.class);
//                startActivity(intent);
                // 内部程序
//                Intent intent = new Intent("com.sanhe.demo02.ACTION_START");
                // 外部网页
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse("https://weibo.com"));
                startActivity(intent);
            }
        });
    }

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

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        Log.i("MainActivity2",String.valueOf(item.getItemId()));
        Toast.makeText(MainActivity2.this,"点击按钮"+item.getTitle(),Toast.LENGTH_LONG).show();
        return super.onOptionsItemSelected(item);
    }
}
XML 复制代码
        <activity
            android:name=".MainActivity3"
            android:exported="false"
            android:theme="@style/Theme.AppCompat.Light">
            <intent-filter>
                <action android:name="com.sanhe.demo02.ACTION_START" />
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
相关推荐
AgCl234 小时前
MYSQL-6-函数与约束-3/17
android·数据库·mysql
zzb15805 小时前
Fragment 生命周期深度图解:从 onAttach 到 onDetach 完整流程(面试必备)
android·java·面试·安卓
众少成多积小致巨5 小时前
Android 源码查看笔记
android·源码
angerdream5 小时前
Android手把手编写儿童手机远程监控App之前台服务
android
敲代码的瓦龙7 小时前
Android?Activity!!!
android
重生之我在安卓搞音频8 小时前
二、Android 音频框架
android·音视频
studyForMokey9 小时前
【Android面试】Java专题 todo
android·java·面试
代码改善世界9 小时前
【MATLAB初阶】矩阵操作(二):矩阵的运算
android·matlab·矩阵
九皇叔叔9 小时前
MySQL实操指南:复制表及数据复制全解析
android·数据库·mysql
梦想不只是梦与想9 小时前
flutter 与 Android iOS 通信?以及实现原理(一)
android·flutter·ios·methodchannel·eventchannel·basicmessage