首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >活动之间互动的最佳做法

活动之间互动的最佳做法
EN

Stack Overflow用户
提问于 2015-10-20 04:40:48
回答 5查看 699关注 0票数 2

我正在开发一个application,只是为了通过意图验证activities之间的交互。

我创建了5个ImageButton,每个ImageButton都有一个图像。每一个按钮都是一部电影,如果你点击其中的任何一个,都会指向一个新的activity,其中包含电影的概要。带有概要的activity,有一个返回MainActivity (home)的"up navigation“。

我开发的方式留下了非常广泛的项目,因为它创建了六个activities (main activity和5个activities,每部电影一个)和6个layouts。另外,我的apk是1.5mb

有人能帮我提供关于我最小化code的最佳实践的建议吗?或者开发的方法是正确的,并且可以在真正的application中开发。

我很感激!

My MainActivity

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
package luizugliano.com.br.appfilmes;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

}

public void onClickBtVideo01(View view){
    Intent intent = new Intent(getContext(),ActivityVideo01.class);
    startActivity(intent);
}

public void onClickBtVideo02(View view){
    Intent intent = new Intent(getContext(),ActivityVideo02.class);
    startActivity(intent);
}

public void onClickBtVideo03(View view){
    Intent intent = new Intent(getContext(),ActivityVideo03.class);
    startActivity(intent);
}

public void onClickBtVideo04(View view){
    Intent intent = new Intent(getContext(),ActivityVideo04.class);
    startActivity(intent);
}

public void onClickBtVideo05(View view){
    Intent intent = new Intent(getContext(),ActivityVideo05.class);
    startActivity(intent);
}

private Context getContext(){
    return this;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

My ActivityVideo01 (其他活动具有相同的代码,因此我只将其作为示例)

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
package luizugliano.com.br.appfilmes;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

public class ActivityVideo01 extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_activity_video01);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == android.R.id.home) {
        //O método finish encerrará essa activity
        finish();
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

My content_main.xml

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" 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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main" tools:context=".MainActivity">

<TextView android:text="Sinopse - Filmes" android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:textSize="22dp"/>

<HorizontalScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="@dimen/layout_marginTop">

    <ImageButton
        android:layout_width="@dimen/layout_width"
        android:layout_height="@dimen/layout_height"
        android:id="@+id/imageButton01"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center_vertical"
        android:background="@drawable/btn_img_01"
        android:onClick="onClickBtVideo01"/>

    <ImageButton
        android:layout_width="@dimen/layout_width"
        android:layout_height="@dimen/layout_height"
        android:id="@+id/imageButton02"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="@dimen/layout_marginLeft"
        android:background="@drawable/btn_img_02"
        android:onClick="onClickBtVideo02"/>

    <ImageButton
        android:layout_width="@dimen/layout_width"
        android:layout_height="@dimen/layout_height"
        android:id="@+id/imageButton03"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="@dimen/layout_marginLeft"
        android:background="@drawable/btn_img_03"
        android:onClick="onClickBtVideo03"/>

    <ImageButton
        android:layout_width="@dimen/layout_width"
        android:layout_height="@dimen/layout_height"
        android:id="@+id/imageButton04"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="@dimen/layout_marginLeft"
        android:background="@drawable/btn_img_04"
        android:onClick="onClickBtVideo04"/>

    <ImageButton
        android:layout_width="@dimen/layout_width"
        android:layout_height="@dimen/layout_height"
        android:id="@+id/imageButton05"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="@dimen/layout_marginLeft"
        android:background="@drawable/btn_img_05"
        android:onClick="onClickBtVideo05"/>

</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>

My content_activity_video01.xml (其他布局具有相同的代码,因此我只将其作为示例)

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" 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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_activity_video01"
tools:context="luizugliano.com.br.appfilmes.ActivityVideo01">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Title Synopsis"
    android:id="@+id/textView"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Synopsis"
    android:id="@+id/textView2"
    android:layout_below="@+id/textView"
    android:layout_marginTop="51dp"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />
</RelativeLayout>
EN

回答 5

Stack Overflow用户

发布于 2015-10-20 04:56:52

我建议,您有两个活动,这两个活动将作为容器活动各自的片段。

在你第一次活动的片段中有你的按钮。在你第二次活动的片段里,播放你的电影。

现在,当您单击按钮时,从第一个活动的片段转到第二个活动的片段。使用接口在片段之间进行通信。

这就是我的想法,如果你有更好的方法,请分享。

票数 1
EN

Stack Overflow用户

发布于 2015-10-20 04:57:15

不是采取五种不同的活动,而是采取单个活动,并动态替换文本视图的值,点击按钮。

票数 1
EN

Stack Overflow用户

发布于 2015-10-20 04:59:54

只需标记与content_main.xml中相同的两个xml文件,创建另一个相对布局(最初是gone视图),在图像按钮线性布局下具有高度、宽度匹配父级。当一个图像按钮被按下时,图像按钮的线性布局视图就会消失,而文本视图的相对布局和向上导航的后退按钮就会显示出来。当用户按up导航时,刚刚离开文本视图相对布局,进行向上导航,可见图像按钮的线性布局。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33236960

复制
相关文章
Python 字符串 去中间空格
这招不太灵光,不仅是因为编码的问题(2.x 的版本中使用 repr()可以看到空格对应的编码,用其替换),而且太麻烦,不够灵活 。
py3study
2020/01/11
2.4K0
php用空格分隔字符串,分割字符串空格[通俗易懂]
String[] data = s.split(“@”); // 以@分割字符串,获得@后的值。
全栈程序员站长
2022/11/16
6.3K0
去掉多余的空格---输入一个字符串,字符串中可能包含多个连续的空格,请将多余的空格去掉,只留下一个空格。输入格式
输入一个字符串,字符串中可能包含多个连续的空格,请将多余的空格去掉,只留下一个空格。
莫浅子
2022/11/18
2.7K0
JS去除字符串的空格
JS去除字符串前后空格 //去前后空格 //var LO = data.replace(/(^\s*)|(\s*$)/g, ""); //var LA = data.replace(/(^\s*)|(\s*$)/g, ""); JS去除字符串所有空格 //直接去除所有的空格 var LL = data.replace(/\s+/g,""); 在Kettle做数据清洗中用到的: //Script here //去前后空格 //var LO = LONGITUDE.replace(/(^\s*)|(\s*
手撕代码八百里
2020/07/28
8K0
带空格的字符串反转
就是输入一段文本,然后让你实现字符串反转。 肺炎严重,假期延长,闲的不得不自主学习,然后就日常刷水题。 第一次的方法,简单易懂,就是时间超限了。TLIM #include<bits/stdc++.h> using namespace std; stack<string> st; int main(){ string s; cin>>s; reverse(s.begin(),s.end()); st.push(s); while(getchar() != '\n'){ cin>>s; re
杨鹏伟
2020/09/11
1.3K0
字符串:替换空格
示例 1: 输入:s = "We are happy." 输出:"We%20are%20happy."
代码随想录
2020/09/10
4.6K0
字符串:替换空格
php删除字符串的空格
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/105882.html原文链接:https://javaforall.cn
全栈程序员站长
2022/08/09
3.6K0
PHP去除多余空格 多个连续空格只保留一个
/** 多个连续空格只保留一个 * @param string $string 待转换的字符串 @return unknown */ static public function merge_spa
大师级码师
2021/10/29
2.7K0
关于字符串切割空格
注意: 不要在split方法中写" ", 这样只能匹配到一个空格 String s = "hello world you"; String[] split = s.split("\\s+"); for (String s1 : split) { System.out.println(s1); }
乐心湖
2020/07/31
1.1K0
算法-字符串替换空格
chaibubble
2018/01/02
2.4K0
算法-字符串替换空格
android空格字符串_v1d空格复制
&#160; == &nbsp; == &#xA0; == no-break space (普通的英文半角空格但不换行)
全栈程序员站长
2022/09/30
8260
python 字符串去空格
1、lstrip:删除左边的空格 这个字符串方法,会删除字符串s开始位置前的空格。
全栈程序员站长
2022/09/03
2.6K0
js去除字符串空格
去除字符串内所有的空格:str = str.replace(/\s*/g,””);
青梅煮码
2023/01/16
9.9K0
[785]python去掉字符串中多余的空格
参考:https://www.jianshu.com/p/25def1847697 https://blog.csdn.net/baidu_15113429/article/details/80651091
周小董
2020/04/21
1.5K0
javascript去掉字符串前后空格
使用场景 当我们进行一些页面编辑时,字符串前后的空格,通常是无效的。因此需要在获取信息时,进行过滤。 比如: 输入:[空格][空格]a[空格]b[空格][空格][空格] 得到:a[空格]b 代码如下: 去掉前面的空格 1 function LTrim(str){ 2 var i; 3 for(i=0;i<str.length;i++){ 4 if(str.charAt(i)!=" ") 5 break; 6 } 7
用户1154259
2018/01/17
2.2K0
JavaScript 去除字符串首尾空格
Unsplash 去除字符串首尾空格的方式,主要是利用正则进行替换,这里写了两种方式供大家参考 function trim(string) { if(string.trim) { return string.trim(); }else { let reg = /^\s+|\s+$/g; return string.replace(reg,""); } } if(typeof String.prototype.trim !== "func
Nian糕
2020/05/26
4.2K0
JavaScript 去除字符串首尾空格
Python_去除字符串中的空格
01. strip() 方法 strip() :用于移除字符串头尾指定的字符(默认为空格)或字符序列。 注: 该方法只能删除开头或结尾的字符,不能删除中间部分的字符。 old_data = " a b c d 1 1 3 1 " new_data = old_data.strip() old_data2 = "com.123fa.comsfasf.comasdfrs324.com" new_data2 = old_data2.strip(".com") print(new_data) print(new
用户7741497
2022/03/24
1.8K0
Java字符串去掉空格的几种方法
其中,\s可以匹配空格、制表符、换页符等空白字符 参考:Java正则表达式https://www.runoob.com/java/java-regular-expressions.html
kirin
2020/07/01
4.7K0
深度学习中的模型修剪
本在本文中,我们将介绍深度学习背景下的模型修剪机制。模型修剪是一种丢弃那些不代表模型性能的权重的艺术。精心修剪的网络会使其压缩版本更好,并且它们通常变得适合设备上的部署。
deephub
2020/07/06
1.1K0
深度学习中的模型修剪
利用切片操作去除字符串空格
在写一个函数,利用切片操作,可以去除一个字符串前后的空格,比如字符串' I.m working ',去除前后空格后得到‘I‘m working’
算法与编程之美
2021/11/23
1.9K0

相似问题

C#列表.ConvertAll效率和开销

45

ConvertAll (UWP C#)

13

C# ConvertAll语法

27

带索引的C#列表ConvertAll

18

IEnumerable<T>.ConvertAll和DDD

22
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文