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";

    }

}

我正在尝试获取 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) 放入一个字符串有没有办法做到这一点,还是我必须为每个菜单中的每个按钮手动完成?

4

2 回答 2

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";

    }

}
于 2012-12-16T17:34:14.443 回答
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");
于 2012-12-16T00:52:38.977 回答