2014-09-20 29 views
0

當用戶點擊每個圖片按鈕時,我有一個包含6個圖片按鈕的頁面將進入詳細頁面,該頁面包含帶有額外信息的圖標圖片。問題是如何設置詳細頁面中的圖像對應於用戶點擊的圖像按鈕?例如當用戶點擊詳細信息頁面中的圖像按鈕1時,在詳細信息頁面中的圖像按鈕2上單擊圖像button1時,我們看到按鈕2的圖像,如何在活動頁面代碼中定義此屬性?如何設置嵌套活動中的圖像按鈕?

這裏是對應於6個圖像按鈕我的代碼:

//實現OnClickListener接口 公共類目的地延伸ActionBarActivity 實現View.OnClickListener

{

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 


    setContentView(R.layout.destination); 


    //get the Button reference 
    //Button is a subclass of View 
    //buttonClick if from main.xml "@+id/buttonClick" 
    View btnClick = findViewById(R.id.image1); 
    View btnClick2 = findViewById(R.id.image2); 
    View btnClick3 = findViewById(R.id.image3); 
    View btnClick4 = findViewById(R.id.image4); 
    View btnClick5 = findViewById(R.id.image5); 
    View btnClick6 = findViewById(R.id.image6); 
    btnClick.setOnClickListener(this); 
    btnClick2.setOnClickListener(this); 
    btnClick3.setOnClickListener(this); 
    btnClick4.setOnClickListener(this); 
    btnClick5.setOnClickListener(this); 
    btnClick6.setOnClickListener(this); 
} 

//override the OnClickListener interface method 
@Override 
public void onClick(View arg0) { 
    if (arg0.getId() == R.id.image1) { 
     //define a new Intent for the second Activity 
     Intent intent = new Intent(this, DestinationTherapy.class); 
     //start the second Activity 
     this.startActivity(intent); 
    } else if (arg0.getId() == R.id.image2) { 
     //define a new Intent for the second Activity 
     Intent intent = new Intent(this, DestinationDetail.class); 
     //start the second Activity 
     this.startActivity(intent); 
    } else if (arg0.getId() == R.id.image3) { 
     //define a new Intent for the second Activity 
     Intent intent = new Intent(Destination.this, DestinationDetail.class); 
     //start the second Activity 
     this.startActivity(intent); 
    } else if (arg0.getId() == R.id.image4) { 
     //define a new Intent for the second Activity 
     Intent intent = new Intent(Destination.this, DestinationDetail.class); 
     //start the second Activity 
     this.startActivity(intent); 
    } else if (arg0.getId() == R.id.image5) { 
     //define a new Intent for the second Activity 
     Intent intent = new Intent(Destination.this, DestinationDetail.class); 
     //start the second Activity 
     this.startActivity(intent); 
    } else if (arg0.getId() == R.id.image6) { 
     //define a new Intent for the second Activity 
     Intent intent = new Intent(Destination.this, DestinationDetail.class); 
     //start the second Activity 
     this.startActivity(intent); 
    } 
} 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.detail, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 
    if (id == R.id.action_settings) { 
     startActivity(new Intent(this, Setting.class)); 
     return true; 
    } else if (id == R.id.action_menu) { 
     startActivity(new Intent(this, MainActivity.class)); 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

}

目的地詳情活動:

public class DestinationDetail extends ActionBarActivity 
    implements View.OnClickListener 

{

View btnClick = findViewById(R.id.emergency4); 
    btnClick.setOnClickListener(DestinationDetail.this); 
    View btnClick1 = findViewById(R.id.back2); 
    btnClick1.setOnClickListener(DestinationDetail.this); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.detail, menu); 
    return true; 
} 

public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 
    if (id == R.id.action_settings) { 
     startActivity(new Intent(this, SettingActivity.class)); 
     return true; 
    } 
    if (id == R.id.action_menu) { 
     startActivity(new Intent(this, MainActivity.class)); 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

public void onClick(View arg0) { 
    if (arg0.getId() == R.id.emergency4) { 
     //define a new Intent for the second Activity 
     Intent intent = new Intent(this, EmergencyCall.class); 
     //start the second Activity 
     this.startActivity(intent); 
    } else if (arg0.getId() == R.id.back2) { 
      //define a new Intent for the second Activity 
      Intent intent = new Intent(this, Destination.class); 
      //start the second Activity 
      this.startActivity(intent); 
     } 
    } 
} 

回答

0
  • 傳遞圖像的資源ID在Intent

Destination活動,在onClick()寫一樣的東西:

public void onClick(View view) { 
    int id = view.getId(); 
    // ... 
    if (id == R.id.image1) { 
     Intent intent = new Intent(this, DestinationTherapy.class); 
     intent.putExtra("res_id", R.drawable.image1);  
     startActivity(intent); 
    } else if (//... 
} 

R.drawable.image1更改爲適當的res ID。在你的情況下,每個圖像都會有所不同,所以你需要一種if/switch結構。

DestinationDetail獲得圖像的處理,它設置爲

setContentView(...); 
ImageView iv = (ImageView) findViewById(R.id.my_detail_image_view); 
int resId = getIntent().getIntExtra("res_id", 0); 
iv.setImageResource(resId); 

您必須DestinationDetail

澄清與您的ImageView的相應的視圖ID改變R.id.my_detail_image_view(視圖id VS資源ID ):視圖ID是您在xml中定義的一個像android:id="@+id/my_view_id"的視圖。當您將圖像放入可繪製文件夾時,圖像的res id會自動生成。您可以使用它在與R.id.image_name_without_extension的java或與@drawable/image_name_without_extension

+0

謝謝,但我不明白我應該把代碼放在DestinationDetail頁面? – 2014-09-20 20:02:27

+0

第二個片段放在DestinationDetail的onCreate()中。適當地修改你的ID – 2014-09-20 20:03:59

+0

PS:我沒有看到對setContentView()的調用...你應該擁有它,否則你的視圖層次結構將不會被綁定到窗口,你不會看到任何東西 – 2014-09-20 20:05:12