2017-03-02 136 views
0

我有許多不同顏色名稱的按鈕。選擇一個按鈕android

yellow - red - blue 

我想,當一個用戶點擊它創建它周圍的邊框(選擇按鈕),在我的活動結束後我還有一個按鈕,保存所選顏色的用戶。

<Button 
     android:text="Yellow" 
     android:layout_width="111dp" 
     android:layout_height="wrap_content" 
     android:id="@+id/button1" /> 

<Button 
     android:text="Red" 
     android:layout_width="111dp" 
     android:layout_height="wrap_content" 
     android:id="@+id/button2" /> 


<Button 
     android:text="SAVE" 
     android:layout_width="111dp" 
     android:layout_height="wrap_content" 
     android:id="@+id/buttonsave" /> 

的Java

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

    setContentView(R.layout.color); 

    Button btnYellow; 
    btnYellow = (Button) findViewById(R.id.button1); 
    Button btnRed; 
    btnRed = (Button) findViewById(R.id.button2); 


    Intent intent = getIntent(); 
    String url2 = intent.getStringExtra("image"); 




    btnYellow.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 

     } 
    }); 


} 

我怎麼能選擇一個按鈕,當用戶點擊它,並得到一個值(紅,綠,RED1),當用戶點擊保存?

+0

您可以點擊事件更改按鈕的背景顏色 –

+2

您需要的單選按鈕或複選框,而不是常規按鈕 –

+0

首先我想選擇一個按鈕,當用戶點擊,如:紅色。他們,當他點擊「保存」時,我會顯示一條消息,例如「紅色」。 @Chetan –

回答

1

廣場每個按鈕在FrameLayout。這會給按鈕一個邊框。更改FrameLayout上的背景顏色將更改按鈕的邊框。

<FrameLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

    <Button 
     android:id="@+id/btnYellow" 
     android:layout_width="111dp" 
     android:layout_height="wrap_content" 
     android:text="Yellow" /> 
</FrameLayout> 

設置,看起來像下面的按鈕的onClickListener,但不要使用硬編碼的顏色 - 這只是一個例子。 mLastClicked是一個成員變量,定義爲Button mLastClicked

@Override 
public void onClick(View view) { 

    if (mLastClicked !=null) { 
     ((FrameLayout) mLastClicked.getParent()).setBackgroundColor(0xFFFFFFFF); 
    } 
    mLastClicked = (Button) view; 
    switch (view.getId()) { 

     case R.id.btnYellow: 
      ((FrameLayout) view.getParent()).setBackgroundColor(0xFFFFFF00); 
      break; 

     case R.id.btnRed: 
      // Similar to yellow 
      break; 

     case R.id.btnSave: 
      // Do something with mLastClicked to save it 
      break; 
    } 
} 
1

您可以定義按鈕的形狀給它一個邊框,使用元素(命名該文件your.xml,並將其放置在res /繪項目):

,並請參閱本link

相關問題