ImageButton photo;

photo = (ImageButton) findViewById(R.id.imageButton);

photo.setImageResource(R.drawable.hum);

樂高機器人控制使用下拉式選單選擇機器人

由於串列埠的class無法開啟第二個,無法利用下拉式選擇跳到所要的機器人介面

本來想要用判斷式,分別跳不同的layout也不行

因此採用更換傳送指令與圖片的方式,利用判斷式將下拉式所選擇的選項字串intent到下一個Activity

使用a.equal(b)的函式判斷字串是否一樣

ex:

String temp="",hum="Humanoid robot",sco="Scorpion";

if(temp.equals(hum))photo.setImageResource(R.drawable.hum);

else photo.setImageResource(R.drawable.sco);

整個class也只需要上下左右攻擊停止這六個按鈕

之前還新增到十二的按鈕,實在有點多

用判斷式之後,多餘的程式碼也能刪除

 

另外,在Intent的部分

以下為按下"ok"這個按鈕的動作

private Button.OnClickListener button_listener = new Button.OnClickListener(){

public void onClick(View v){

Intent intent = new Intent();//建立一個新的Intent實體

intent.setClass(console_home.this, ConsoleActivity.class);//設定Intent所要跳向的class

Bundle robot = new Bundle();//建立Bundle物件

robot.putString("robot", temp);//將temp的字串放入robot變數中,作後接收端使用

intent.putExtras(robot);//用Bundle的實體來做Intent的傳送

startActivity(intent);

}

};

接收端的部分

Intent get_intent = this.getIntent();

Bundle get_bundle = get_intent.getExtras(); 

IntentReceive = get_bundle.getString("robot").toString(); //接收端所收到的字串

arrow
arrow
    全站熱搜

    glitterHsin 發表在 痞客邦 留言(0) 人氣()