得到它的工作,但它比我想成爲更長。 新的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)放在一個字符串中有沒有辦法做到這一點,或者我將不得不手動爲每個菜單中的每個按鈕?
我能做到這一點我需要的是如何從每個按鈕的ID的獲取文本,並把它們放到一個字符串,因爲有不同的菜單一百多按鈕我必須做 –
'((Button)findViewById(R.id.btn_id))。getId();'? 編輯:當一個按鈕被按下時,提供者添加了一個監聽器,'onClick(View v)'在監聽器上被觸發。你可以通過'v.getViewId()'獲取被按下的按鈕的ID。 編輯2:對不起,'getId()'。 'getViewId()'用於'ListView's。 –
謝謝我會嘗試 –