安卓:Simpify许多类和XMLSimpify、XML

2023-09-04 07:35:19 作者:我掐的比你早你爱的比我少

编辑:我不能得到这个正常工作。可能是因为我不知道我在做什么。不管怎么说,这是我的code。如果有人可以帮助,我会非常感激:我需要得到它来显示电池的心情图像对应的模...

主题:

 包com.cydeon.plasmamodz;

进口android.app.ActionBar;
进口android.app.Activity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.Button;
进口android.widget.ImageView;

公共类主题延伸活动{

@覆盖
保护无效的onCreate(包savedInstanceState){
    // TODO自动生成方法存根
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.themes);
    动作条动作条= getActionBar();
    actionBar.hide();
    按键加=(按钮)findViewById(R.id.button1);
    按钮蓝色=(按钮)findViewById(R.id.button2);
    Plus.setOnClickListener(新View.OnClickListener(){

        @覆盖
        公共无效的onClick(查看为arg0){
            意图I =新的意图(Themes.this,Bmod.class);
            i.putExtra(drawableResource,R.drawable.blue);
            Themes.this.startActivity(ⅰ);

        }
    });

    Blue.setOnClickListener(新View.OnClickListener(){

        @覆盖
        公共无效的onClick(查看为arg0){
            意图=新的意图(Themes.this,Bmod.class);
            a.putExtra(drawableResource1,R.drawable.plus);
            Themes.this.startActivity(一);

        }
    });


}

}
 

Bmods:

 包com.cydeon.plasmamodz;

进口android.app.Activity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.widget.ImageView;

公共类BMOD延伸活动{

@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.battery);

    意图I = getIntent();
    INT drawableResource = i.getIntExtra(drawableResource,R.drawable.blue);
    ImageView的IMG =(ImageView的)findViewById(R.id.iv1);
    img.setImageResource(R.drawable.blue);

    意图= getIntent();
    INT drawableResource1 = a.getIntExtra(drawableResource1,R.drawable.plus);
    ImageView的IMG1 =(ImageView的)findViewById(R.id.iv1);
    img1.setImageResource(R.drawable.plus);
}



}
 

电池(XML):

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直>

<按钮
    机器人:ID =@ + ID / bInstall
    机器人:layout_width =300dp
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignParentBottom =真
    机器人:layout_alignParentLeft =真
    机器人:文本=安装/>

<按钮
    机器人:ID =@ + ID / bReturn
    机器人:layout_width =300dp
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignParentBottom =真
    机器人:layout_alignParentRight =真
    机器人:文本=返回/>

< ImageView的
    机器人:ID =@ + ID / IV1
    机器人:layout_width =match_parent
    机器人:layout_height =800DP/>

< / RelativeLayout的>
 
安卓应用开发 文件结构了解 运行第一个app

解决方案

 意向书我=新的意图(这一点,BaseClassForMod);
i.putExtra(drawableResource,R.drawable.this_mod_drawable);
startActivity(ⅰ);
 

然后在该活动的onCreate():

 意图I = getIntent();
INT drawableResource = i.getIntExtra(drawableResource,R.drawable.default);
//获取关于你的ImageView ...
imageView.setImageResource(drawableResource);
 

不信任此code编译,但是这是总体思路。使用相同的活动为所有这些,沿着正确的资源ID传递的意图(例如MOD1,派人MOD1可绘制的ID),然后在活动中,检查该资源ID,并通过编程设定。

Edit: I cannot get this to work correctly. Probably because I have no idea what I am doing. Anyways, here's my code. If anyone could help, I'd be very grateful: I needs to get it to display the battery mood image for the corresponding mod...

Themes:

package com.cydeon.plasmamodz;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class Themes extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.themes);
    ActionBar actionBar = getActionBar();
    actionBar.hide();
    Button Plus = (Button) findViewById(R.id.button1);
    Button Blue = (Button) findViewById(R.id.button2);
    Plus.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Intent i = new Intent(Themes.this, Bmod.class);
            i.putExtra("drawableResource", R.drawable.blue);
            Themes.this.startActivity(i);

        }
    });

    Blue.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Intent a = new Intent(Themes.this, Bmod.class);
            a.putExtra("drawableResource1", R.drawable.plus);
            Themes.this.startActivity(a);

        }
    });


}

}

Bmods:

package com.cydeon.plasmamodz;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;

public class Bmod extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.battery);

    Intent i = getIntent();
    int drawableResource = i.getIntExtra("drawableResource", R.drawable.blue);
    ImageView img = (ImageView) findViewById(R.id.iv1);
    img.setImageResource(R.drawable.blue);

    Intent a = getIntent();
    int drawableResource1 = a.getIntExtra("drawableResource1", R.drawable.plus);
    ImageView img1 = (ImageView) findViewById(R.id.iv1);
    img1.setImageResource(R.drawable.plus);
}



}

battery(xml):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout                          xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
    android:id="@+id/bInstall"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:text="Install" />

<Button
    android:id="@+id/bReturn"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:text="Return" />

<ImageView
    android:id="@+id/iv1"
    android:layout_width="match_parent"
    android:layout_height="800dp" />

</RelativeLayout>

解决方案

Intent i = new Intent(this, BaseClassForMod);
i.putExtra("drawableResource", R.drawable.this_mod_drawable);
startActivity(i);

Then in that Activity's onCreate():

Intent i = getIntent();
int drawableResource = i.getIntExtra("drawableResource", R.drawable.default);
//Get a reference to your ImageView...
imageView.setImageResource(drawableResource);

Don't trust this code to compile, but that's the general idea. Use the same Activity for all of them, pass along the proper resource ID in the intent (e.g. for mod1, send the drawable ID for mod1), then in the activity, check for that resource ID and set it programmatically.