将Id名称放在字符串中

时间:2012-12-16 00:45:55

标签: android string

让它工作但比我想要的时间更长。  新Java:

package com.example.musicbynumbers;



import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;

public class majorScales extends Activity implements View.OnClickListener  {
    Button aflatmaj, amaj, bflatmaj, bmaj, cmaj, dflatmaj, dmaj, eflatmaj, emaj, fmaj, fsharpmaj, gmaj;  
    ImageButton mainMenu;
    Intent j;
    String scaleName;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.majorscales);
        j  = new Intent(this, display.class);
        mainMenu = (ImageButton) findViewById(R.id.imagelogo);
        aflatmaj = (Button) findViewById(R.id.aflatmajb);
        amaj = (Button) findViewById(R.id.amajb);
        bflatmaj = (Button) findViewById(R.id.bflatmajb);
        bmaj = (Button) findViewById(R.id.bmajb);
        cmaj = (Button) findViewById(R.id.cmajb);
        dflatmaj = (Button) findViewById(R.id.dflatmajb);
        dmaj = (Button) findViewById(R.id.dmajb);
        eflatmaj = (Button) findViewById(R.id.eflatmajb);
        emaj = (Button) findViewById(R.id.emajb);
        fmaj = (Button) findViewById(R.id.fmajb);
        fsharpmaj = (Button) findViewById(R.id.fsharpmajb);
        gmaj = (Button) findViewById(R.id.gmajb);
        mainMenu.setOnClickListener(this);
        aflatmaj.setOnClickListener(this);
        amaj.setOnClickListener(this);
        bflatmaj.setOnClickListener(this);
        bmaj.setOnClickListener(this);
        cmaj.setOnClickListener(this);
        dflatmaj.setOnClickListener(this);
        dmaj.setOnClickListener(this);
        eflatmaj.setOnClickListener(this);
        emaj.setOnClickListener(this);
        fmaj.setOnClickListener(this);
        fsharpmaj.setOnClickListener(this);
        gmaj.setOnClickListener(this);



    }






         @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                switch(arg0.getId()){
                case R.id.imagelogo:
                    Intent i =  new Intent(majorScales.this, MainMenu.class);
                    startActivity(i);
                    break;
                case R.id.aflatmajb:
                    scaleName = "aflatmaj";
                    j.putExtra("key", scaleName);

                    startActivity(j);
                    break;
                case R.id.amajb:
                    scaleName = "aflatmaj";
                    j.putExtra("key", scaleName);
                    startActivity(j);
                    break;
                case R.id.bflatmajb:
                    scaleName = "aflatmaj";
                    j.putExtra("key", scaleName);
                    startActivity(j);
                    break;
                case R.id.bmajb:
                    scaleName = "aflatmaj";
                    j.putExtra("key", scaleName);
                    startActivity(j);
                    break;
                case R.id.cmajb:
                    scaleName = "aflatmaj";
                    j.putExtra("key", scaleName);
                    startActivity(j);
                    break;
                case R.id.dflatmajb:
                    scaleName = "aflatmaj";
                    j.putExtra("key", scaleName);
                    startActivity(j);
                    break;
                case R.id.dmajb:
                    scaleName = "aflatmaj";
                    j.putExtra("key", scaleName);
                    startActivity(j);
                    break;
                case R.id.eflatmajb:
                    scaleName = "aflatmaj";
                    j.putExtra("key", scaleName);
                    startActivity(j);
                    break;
                case R.id.emajb:
                    scaleName = "aflatmaj";
                    j.putExtra("key", scaleName);
                    startActivity(j);
                    break;
                case R.id.fmajb:
                    scaleName = "aflatmaj";
                    j.putExtra("key", scaleName);
                    startActivity(j);
                    break;
                case R.id.fsharpmajb:
                    scaleName = "aflatmaj";
                    j.putExtra("key", scaleName);
                    startActivity(j);
                    break;
                case R.id.gmajb:
                    scaleName = "aflatmaj";
                    j.putExtra("key", scaleName);
                    startActivity(j);
                    break;




    }

}
}



package com.example.musicbynumbers;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageButton;
import android.widget.ImageView;

public class display extends Activity {
    ImageView displayScale;
    ImageButton logoButton;
    String gotScale;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.display);
        Initalize();
        gotScale = getIntent().getStringExtra("key");
        ImageView displayScale = (ImageView) findViewById(R.id.displayImage);
        int id = getResources().getIdentifier("com.example.musicbynumbers:drawable/" + gotScale, null, null);
        displayScale.setImageResource(id);
    }

    private void Initalize() {
        // TODO Auto-generated method stub
        displayScale = (ImageView) findViewById(R.id.displayImage);
        logoButton = (ImageButton) findViewById(R.id.imagelogo);
        gotScale = "ic_launcher.png";

    }

}

我正在尝试获取id名称并将其放入字符串中,然后转到另一个活动并打开具有相应文件名的资源图片

例如。

r.id.x  得到x并将其放在字符串y中 然后将其发送到不同的活动 我如何获得x并将其放入字符串?

我知道有一个简单的答案,因为我对编程非常陌生。

package com.example.musicbynumbers;

import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;

public class majorScales extends Activity implements View.OnClickListener  {
    Button aflatmaj, amaj, bflatmaj, bmaj, cmaj, dflatmaj, dmaj, eflatmaj, emaj, fmaj, fsharpmaj, gmaj;  
    ImageButton mainMenu;
    Intent j;
    String scaleName;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.majorscales);
        j  = new Intent(this, display.class);
        mainMenu = (ImageButton) findViewById(R.id.imagelogo);
        aflatmaj = (Button) findViewById(R.id.aflatmajb);
        amaj = (Button) findViewById(R.id.amajb);
        bflatmaj = (Button) findViewById(R.id.bflatmajb);
        bmaj = (Button) findViewById(R.id.bmajb);
        cmaj = (Button) findViewById(R.id.cmajb);
        dflatmaj = (Button) findViewById(R.id.dflatmajb);
        dmaj = (Button) findViewById(R.id.dmajb);
        eflatmaj = (Button) findViewById(R.id.eflatmajb);
        emaj = (Button) findViewById(R.id.emajb);
        fmaj = (Button) findViewById(R.id.fmajb);
        fsharpmaj = (Button) findViewById(R.id.fsharpmajb);
        gmaj = (Button) findViewById(R.id.gmajb);
        mainMenu.setOnClickListener(this);
        aflatmaj.setOnClickListener(this);
        amaj.setOnClickListener(this);
        bflatmaj.setOnClickListener(this);
        bmaj.setOnClickListener(this);
        cmaj.setOnClickListener(this);
        dflatmaj.setOnClickListener(this);
        dmaj.setOnClickListener(this);
        eflatmaj.setOnClickListener(this);
        emaj.setOnClickListener(this);
        fmaj.setOnClickListener(this);
        fsharpmaj.setOnClickListener(this);
        gmaj.setOnClickListener(this);



    }




         @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                switch(arg0.getId()){
                case R.id.imagelogo:
                    Intent i =  new Intent(majorScales.this, MainMenu.class);
                    startActivity(i);
                    break;
                case R.id.aflatmajb:
                    scaleName = 

                    startActivity(j);
                    break;
                case R.id.amajb:

                    startActivity(j);
                    break;
                case R.id.bflatmajb:

                    startActivity(j);
                    break;
                case R.id.bmajb:

                    startActivity(j);
                    break;
                case R.id.cmajb:

                    startActivity(j);
                    break;
                case R.id.dflatmajb:

                    startActivity(j);
                    break;
                case R.id.dmajb:
                    ;
                    startActivity(j);
                    break;
                case R.id.eflatmajb:
                    ;
                    startActivity(j);
                    break;
                case R.id.emajb:
                    ;
                    startActivity(j);
                    break;
                case R.id.fmajb:
                    ;
                    startActivity(j);
                    break;
                case R.id.fsharpmajb:
                    ;
                    startActivity(j);
                    break;
                case R.id.gmajb:
                    ;
                    startActivity(j);
                    break;



    }

}
}

例如在“R.id.aflatmajb”的开关盒中,我想要r.id之后的部分。 (aflatmajb)要放在一个字符串中有没有办法做到这一点,还是我必须手动为每个菜单中的每个按钮做一次?

2 个答案:

答案 0 :(得分:0)

您的意思是在转换时在活动之间传递数据吗? 你做了

// This is a String, you can use a lot of data types
String theData = "This is some data I need to send along";

// Create a new Intent that will start NextActivity
Intent i = new Intent(this, NextActivity.class);

// Add the data to the Intent using any key you want (which is how you reference the data)
i.putExtra("key", theData);

// Start the activity
startActivity(i);

然后,您将从第二项活动中的Intent获取数据:

// getIntent() returns the Intent that was used to start the current Activity
String theData = getIntent().getStringExtra("key");

答案 1 :(得分:0)

让它工作但比我想要的时间更长。  新Java:

package com.example.musicbynumbers;



import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;

public class majorScales extends Activity implements View.OnClickListener  {
    Button aflatmaj, amaj, bflatmaj, bmaj, cmaj, dflatmaj, dmaj, eflatmaj, emaj, fmaj, fsharpmaj, gmaj;  
    ImageButton mainMenu;
    Intent j;
    String scaleName;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.majorscales);
        j  = new Intent(this, display.class);
        mainMenu = (ImageButton) findViewById(R.id.imagelogo);
        aflatmaj = (Button) findViewById(R.id.aflatmajb);
        amaj = (Button) findViewById(R.id.amajb);
        bflatmaj = (Button) findViewById(R.id.bflatmajb);
        bmaj = (Button) findViewById(R.id.bmajb);
        cmaj = (Button) findViewById(R.id.cmajb);
        dflatmaj = (Button) findViewById(R.id.dflatmajb);
        dmaj = (Button) findViewById(R.id.dmajb);
        eflatmaj = (Button) findViewById(R.id.eflatmajb);
        emaj = (Button) findViewById(R.id.emajb);
        fmaj = (Button) findViewById(R.id.fmajb);
        fsharpmaj = (Button) findViewById(R.id.fsharpmajb);
        gmaj = (Button) findViewById(R.id.gmajb);
        mainMenu.setOnClickListener(this);
        aflatmaj.setOnClickListener(this);
        amaj.setOnClickListener(this);
        bflatmaj.setOnClickListener(this);
        bmaj.setOnClickListener(this);
        cmaj.setOnClickListener(this);
        dflatmaj.setOnClickListener(this);
        dmaj.setOnClickListener(this);
        eflatmaj.setOnClickListener(this);
        emaj.setOnClickListener(this);
        fmaj.setOnClickListener(this);
        fsharpmaj.setOnClickListener(this);
        gmaj.setOnClickListener(this);



    }






         @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                switch(arg0.getId()){
                case R.id.imagelogo:
                    Intent i =  new Intent(majorScales.this, MainMenu.class);
                    startActivity(i);
                    break;
                case R.id.aflatmajb:
                    scaleName = "aflatmaj";
                    j.putExtra("key", scaleName);

                    startActivity(j);
                    break;
                case R.id.amajb:
                    scaleName = "aflatmaj";
                    j.putExtra("key", scaleName);
                    startActivity(j);
                    break;
                case R.id.bflatmajb:
                    scaleName = "aflatmaj";
                    j.putExtra("key", scaleName);
                    startActivity(j);
                    break;
                case R.id.bmajb:
                    scaleName = "aflatmaj";
                    j.putExtra("key", scaleName);
                    startActivity(j);
                    break;
                case R.id.cmajb:
                    scaleName = "aflatmaj";
                    j.putExtra("key", scaleName);
                    startActivity(j);
                    break;
                case R.id.dflatmajb:
                    scaleName = "aflatmaj";
                    j.putExtra("key", scaleName);
                    startActivity(j);
                    break;
                case R.id.dmajb:
                    scaleName = "aflatmaj";
                    j.putExtra("key", scaleName);
                    startActivity(j);
                    break;
                case R.id.eflatmajb:
                    scaleName = "aflatmaj";
                    j.putExtra("key", scaleName);
                    startActivity(j);
                    break;
                case R.id.emajb:
                    scaleName = "aflatmaj";
                    j.putExtra("key", scaleName);
                    startActivity(j);
                    break;
                case R.id.fmajb:
                    scaleName = "aflatmaj";
                    j.putExtra("key", scaleName);
                    startActivity(j);
                    break;
                case R.id.fsharpmajb:
                    scaleName = "aflatmaj";
                    j.putExtra("key", scaleName);
                    startActivity(j);
                    break;
                case R.id.gmajb:
                    scaleName = "aflatmaj";
                    j.putExtra("key", scaleName);
                    startActivity(j);
                    break;




    }

}
}



package com.example.musicbynumbers;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageButton;
import android.widget.ImageView;

public class display extends Activity {
    ImageView displayScale;
    ImageButton logoButton;
    String gotScale;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.display);
        Initalize();
        gotScale = getIntent().getStringExtra("key");
        ImageView displayScale = (ImageView) findViewById(R.id.displayImage);
        int id = getResources().getIdentifier("com.example.musicbynumbers:drawable/" + gotScale, null, null);
        displayScale.setImageResource(id);
    }

    private void Initalize() {
        // TODO Auto-generated method stub
        displayScale = (ImageView) findViewById(R.id.displayImage);
        logoButton = (ImageButton) findViewById(R.id.imagelogo);
        gotScale = "ic_launcher.png";

    }

}