Android学习0819<三>(Activity的操作)

FrameLayout–帧布局

相当于动画的一帧一帧的覆盖原来的控件

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <Button
        android:layout_height="200dp"
        android:layout_width="200dp"
        android:background="#00df00"/>
    <Button
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#ffee00"/>
</FrameLayout>

这里写图片描述

TableLayout–表格布局

属性均写在 TableLayouot 里面
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:collapseColumns="1"
    android:shrinkColumns="1"
    android:stretchColumns="1"
  >

这里写图片描述

collapsColumns隐藏列,下标从0开始
android:collapseColumns=”1”
这里写图片描述
stretchColumns扩展列,下标从0开始,该列可以向行方向伸展,最多可占据一整行。
android:stretchColumns=”0”
这里写图片描述
shrinkColumns收缩,下标从0开始,当该列子控件的内容太多,已经挤满所在行,那么该子控件的内容将往列方向显示。
android:shrinkColumns=”1”
这里写图片描述
单元格属性
android:layout_span=”1” 指定该单元格占据的列数(未指定时,为1)、
这里写图片描述
android:layout_column =“xx“指定该单元格在第几列显示
这里写图片描述

Activity的操作

1、直接启动activity
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);

  protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hide);
        Button button_next=(Button)findViewById(R.id.button_next);
        button_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent_next=new Intent(ThirdActivity.this,MainActivity.class);
                startActivity(intent_next);
            }
        });
    }

这里写图片描述
>

2、带有返回值得启动activity
API中有介绍

第一个界面调用startActivityForResult(intent,requestCode)
第二个界面先调用getIntent()得到启动的intent
在关闭第二个界面前先封装数据
intent.putExtra(“secondbackdata”,editText.getText().toStrig);
设置resultCode setResult(RESULT_OK,intent);
关闭第二个界面
第一个界面准备接收数据重写
onActivityResult(requestCode,resultCode,Intent data)
判断resultCode然后从data中取出数据
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
startActivityForResult(intent,0x23);

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView=(TextView)findViewById(R.id.textView1);
        Button button=(Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getApplicationContext(),SecondActivity.class);
                startActivityForResult(intent, 1);
            }
        });
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch(requestCode){
            case 1:if (resultCode==RESULT_OK){
                String returnnddate=data.getStringExtra("respond");
                Log.d("My Application",returnnddate);
                textView.setText(returnnddate);
            }
                break;
            default:
        }
    }
setContentView(R.layout.activity_second);
        final EditText editText=(EditText)findViewById(R.id.edit_text);
        Button button=(Button)findViewById(R.id.button_back);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.putExtra("respond",editText.getText().toString());
                setResult(RESULT_OK,intent);
                SecondActivity.this.finish();
            }
        });

这里写图片描述

设置全屏,横、竖屏切换

1、android:theme=”@android:style/Theme.NoTitleBar.Fullscreen”设置全屏

<activity
            android:name=".SecondActivity"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
           <intent-filter>
               <action android:name="com.example.administrator.myapplication"/>
               <category android:name="android.intent.category.LAUNCHER"/>

           </intent-filter>
            </activity>

这里写图片描述

2、requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
注意的是通过这种方法实现的时候需要将以上两方法执行在setContentView之前才有效。(有待。。。。)
这里写图片描述

横竖屏切换
android:screenOrientation=”xx” 设置横竖屏切换
android:screenOrientation=”landscape”设置为横屏

 <activity
            android:name=".SecondActivity"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
            android:screenOrientation="landscape">

           <intent-filter>
               <action android:name="com.example.administrator.myapplication"/>
               <category android:name="android.intent.category.LAUNCHER"/>

           </intent-filter>
            </activity>

这里写图片描述

android:screenOrientation=”portrait”设置为竖屏

<activity
            android:name=".ThirdActivity"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="com.example.administrator.myapplication"/>
                <category android:name="android.intent.category.DEFAULT"/>

            </intent-filter>
        </activity>

这里写图片描述

Android的四大组件

Activity Service ContentProvider BroadcastReceiver(五大组件包括Intent)
Intent Android中的进行通讯的工具,类似于包裹
属性: Component
action 启动某一个特定的应用,例如:打电话
隐式启动Activity
被启动的界面中在manifest中添加intent_filter,在intent_filter中必须添加action,action_name自己对应,category必须填写default的
在启动界面中
category

从第二个界面返回值给第一个界面

SecondActivity界面上的代码:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        final EditText editText=(EditText)findViewById(R.id.edit_text);
        Button button=(Button)findViewById(R.id.button_back);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.putExtra("respond",editText.getText().toString());
                setResult(RESULT_OK,intent);
                SecondActivity.this.finish();
            }
        });
        Button button5=(Button)findViewById(R.id.button_hide);
        button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setAction("com.example.administrator.myapplication");
                intent.putExtra("respond",editText.getText().toString());
                setResult(RESULT_OK,intent);
                SecondActivity.this.finish();
                startActivity(intent);
            }
        });
        Log.d("My Application","第二个界面运行到onCreate");
    }

MainActivity 上的代码:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        requestWindowFeature(Window.FEATURE_NO_TITLE);
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        textView=(TextView)findViewById(R.id.textView1);
        Button button=(Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getApplicationContext(),SecondActivity.class);
                startActivityForResult(intent, 1);
            }
        });
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch(requestCode){
            case 1:if (resultCode==RESULT_OK){
                String returnnddate=data.getStringExtra("respond");
                Log.d("My Application",returnnddate);
                textView.setText(returnnddate);
            }
                break;
            default:
        }
    }

这里写图片描述

调用拨打电话界面

MainActivity 中protected void onCreate(Bundle savedInstanceState) 里的代码:

 Button button1=(Button)findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setAction(Intent.ACTION_DIAL);
                intent.setData(Uri.parse("tel:10086"));
                startActivity(intent);
            }
        });

这里写图片描述

直接拔打电话

MainActivity 中protected void onCreate(Bundle savedInstanceState) 里的代码:

 Button button2=(Button)findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setAction(Intent.ACTION_CALL);
                intent.setData(Uri.parse("tel:10086"));
                startActivity(intent);
            }
        });

这里写图片描述

发短信

MainActivity 中protected void onCreate(Bundle savedInstanceState) 里的代码:

 Button button3=(Button)findViewById(R.id.button3);
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setAction(Intent.ACTION_SENDTO);
                intent.setData(Uri.parse("smsto:10086"));
                intent.putExtra("sms_body","The SMS text");
                startActivity(intent);
            }
        });

这里写图片描述

打开浏览器浏览网页

MainActivity 中protected void onCreate(Bundle savedInstanceState) 里的代码:

Button button4=(Button)findViewById(R.id.button4);
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                intent.setData(Uri.parse("http://www.baidu.com"));
                startActivity(intent);
            }
        });

这里写图片描述

隐式调用

MainActivity 中protected void onCreate(Bundle savedInstanceState) 里的代码:

 Button button5=(Button)findViewById(R.id.button5);
        button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setAction("com.example.administrator.myapplication");
                startActivity(intent);
            }
        });

AndroidManifest.xml中的代码

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.administrator.myapplication"
    >
    <uses-permission android:name="android.permission.CALL_PHONE"/>
    <uses-permission android:name="android.permission.send_sms"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"

        >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".SecondActivity"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
            android:screenOrientation="portrait">

           <intent-filter>
               <action android:name="com.example.administrator.myapplication"/>
               <category android:name="android.intent.category.LAUNCHER"/>

           </intent-filter>
            </activity>
        <activity
            android:name=".ThirdActivity"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
            android:screenOrientation="landscape">
            <intent-filter>
                <action android:name="com.example.administrator.myapplication"/>
                <category android:name="android.intent.category.DEFAULT"/>

            </intent-filter>
        </activity>
    </application>

</manifest>

这里写图片描述

按钮组件的代码

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
    >

    <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView2" />
    <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView1"
        android:layout_below="@+id/button"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="点击启动第二个界面"
        android:id="@+id/button"
        android:layout_below="@+id/textView2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="22dp" />
    <Button
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="拨打电话"
        android:layout_marginLeft="150dp"
        android:layout_marginTop="40dp"
        android:id="@+id/button1"
/>
    <Button
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="直接拨打电话"
        android:layout_marginLeft="250dp"
        android:layout_marginTop="40dp"
        android:id="@+id/button2"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发短信"
        android:id="@+id/button3"
        android:layout_below="@+id/button1"
        android:layout_toLeftOf="@+id/button4"
        android:layout_toStartOf="@+id/button4" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="web"
        android:id="@+id/button4"
        android:layout_below="@+id/button1"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="隐式启动"
        android:id="@+id/button5"
        android:layout_below="@+id/button"
        android:layout_toLeftOf="@+id/button3"
        android:layout_toStartOf="@+id/button3" />
</RelativeLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Android Studio开发环境搭建中创建一个Activity,你可以按照以下步骤进行操作: 1. 首先,在Android Studio中打开你的项目。 2. 在项目的资源视图中,右键点击"app"文件夹,选择"New",然后选择"Activity"。 3. 在弹出的对话框中,选择你想要创建的Activity类型,比如"Empty Activity"或"Basic Activity"。 4. 给你的Activity命名,并选择相关的选项,比如是否生成布局文件等。 5. 点击"Finish"按钮,Android Studio会自动生成相应的代码和布局文件。 6. 在生成的Activity类中,你可以添加你的业务逻辑和界面设计。 7. 如果需要,在AndroidManifest.xml文件中注册你的Activity,以便在应用程序中能够访问它。 这样,你就成功在Android Studio开发环境中创建了一个Activity。你可以根据你的需求在这个Activity中添加更多的功能和界面设计。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Android开发相关的学习资料](https://download.csdn.net/download/m0_57840386/85431994)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [Android Studio开发环境的搭建](https://blog.csdn.net/weixin_41957626/article/details/127336213)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Android Studio开发环境搭建](https://blog.csdn.net/weixin_64192827/article/details/127044187)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值