2012-12-16 97 views
0

得到它的工作,但它比我想成为更长。 新的Java:把Id名称放在字符串中

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 GET 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)放在一个字符串中有没有办法做到这一点,或者我将不得不手动为每个菜单中的每个按钮?

回答

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"); 
+0

我能做到这一点我需要的是如何从每个按钮的ID的获取文本,并把它们放到一个字符串,因为有不同的菜单一百多按钮我必须做 –

+0

'((Button)findViewById(R.id.btn_id))。getId();'? 编辑:当一个按钮被按下时,提供者添加了一个监听器,'onClick(View v)'在监听器上被触发。你可以通过'v.getViewId()'获取被按下的按钮的ID。 编辑2:对不起,'getId()'。 'getViewId()'用于'ListView's。 –

+0

谢谢我会尝试 –

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

    } 

}