2014-02-12 98 views
-2

我是一個完全noob與Java代碼,但我想學習。 現在我設置了鍵盤,要做到這一點我進入3 orizzontal的LinearLayout,並在它的每一個我進入3個按鈕,這一切都在activity_main.xml中這樣我如何更改鍵盤按鈕按下顏色

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/LinearLayout1" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" > 

<EditText 
    android:id="@+id/editText1" 
    android:layout_width="286dp" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="14dp" 
    android:ems="10" 
    android:inputType="numberPassword" > 

    <requestFocus /> 
</EditText> 
<LinearLayout android:orientation="horizontal" 
      android:layout_width="fill_parent" 
      android:layout_height="54.0dip" 
      android:layout_marginLeft="2.0dip" 
      android:layout_marginRight="2.0dip"> 

    <Button android:textStyle="bold" 
      android:id="@+id/one" 
      android:layout_width="0.0sp" 
      android:layout_height="fill_parent" 
      android:layout_marginLeft="2.0dip" 
      android:layout_marginRight="2.0dip" 
      android:layout_weight="1.0" /> 


    <Button android:textStyle="bold" 
      android:id="@+id/two" 
      android:layout_width="0.0sp" 
      android:layout_height="fill_parent" 
      android:layout_marginLeft="2.0dip" 
      android:layout_marginRight="2.0dip" 
      android:layout_weight="1.0" /> 

    <Button android:textStyle="bold" 
      android:id="@+id/three" 
      android:layout_width="0.0sp" 
      android:layout_height="fill_parent" 
      android:layout_marginLeft="2.0dip" 
      android:layout_marginRight="2.0dip" 
      android:layout_weight="1.0" /> 
</LinearLayout> 
<LinearLayout android:orientation="horizontal" 
      android:layout_width="fill_parent" 
      android:layout_height="54.0dip" 
      android:layout_marginLeft="2.0dip" 
      android:layout_marginRight="2.0dip"> 

    <Button android:textStyle="bold" 
      android:id="@+id/four" 
      android:layout_width="0.0sp" 
      android:layout_height="fill_parent" 
      android:layout_marginLeft="2.0dip" 
      android:layout_marginRight="2.0dip" 
      android:layout_weight="1.0" /> 

    <Button android:textStyle="bold" 
      android:id="@+id/five" 
      android:layout_width="0.0sp" 
      android:layout_height="fill_parent" 
      android:layout_marginLeft="2.0dip" 
      android:layout_marginRight="2.0dip" 
      android:layout_weight="1.0" /> 

    <Button android:textStyle="bold" 
      android:id="@+id/six" 
      android:layout_width="0.0sp" 
      android:layout_height="fill_parent" 
      android:layout_marginLeft="2.0dip" 
      android:layout_marginRight="2.0dip" 
      android:layout_weight="1.0" /> 
</LinearLayout> 
<LinearLayout android:orientation="horizontal" 
      android:layout_width="fill_parent" 
      android:layout_height="54.0dip" 
      android:layout_marginLeft="2.0dip" 
      android:layout_marginRight="2.0dip"> 

    <Button android:textStyle="bold" 
      android:id="@+id/seven" 
      android:layout_width="0.0sp" 
      android:layout_height="fill_parent" 
      android:layout_marginLeft="2.0dip" 
      android:layout_marginRight="2.0dip" 
      android:layout_weight="1.0" /> 

    <Button android:textStyle="bold" 
      android:id="@+id/eight" 
      android:layout_width="0.0sp" 
      android:layout_height="fill_parent" 
      android:layout_marginLeft="2.0dip" 
      android:layout_marginRight="2.0dip" 
      android:layout_weight="1.0" /> 

    <Button android:textStyle="bold" 
      android:id="@+id/nine" 
      android:layout_width="0.0sp" 
      android:layout_height="fill_parent" 
      android:layout_marginLeft="2.0dip" 
      android:layout_marginRight="2.0dip" 
      android:layout_weight="1.0" /> 
</LinearLayout> 

</LinearLayout> 

按鈕的每一個背景是明確由一個xml在這樣

<?xml version="1.0" encoding="utf-8"?> 
<selector 
xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_window_focused="false" android:state_enabled="true" android:drawable="@drawable/btn_default_normal_holo_dark" /> 
<item android:state_window_focused="false" android:state_enabled="false" android:drawable="@drawable/btn_default_normal_disable" /> 
<item android:state_pressed="true" android:drawable="@drawable/btn_default_pressed_holo_dark" /> 
<item android:state_focused="true" android:state_enabled="true" android:drawable="@drawable/btn_default_selected" /> 
<item android:state_enabled="true" android:drawable="@drawable/btn_default_normal_holo_dark" /> 
<item android:state_focused="true" android:drawable="@drawable/btn_default_normal_disable_focused" /> 
<item android:drawable="@drawable/btn_default_normal_disable" /> 

</selector> 

和我MainActivity.java繪製是這一

package com.provabottone; 

import android.os.Bundle; 
import android.widget.Button; 
import android.app.Activity; 


public class MainActivity extends Activity { 





private Button mOne; 
private Button mTwo; 
private Button mThree; 
private Button mFour; 
private Button mFive; 
private Button mSix; 
private Button mSeven; 
private Button mEight; 
private Button mNine; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 


      mOne = (Button) findViewById(R.id.one); 
      mTwo = (Button) findViewById(R.id.two); 
      mThree = (Button) findViewById(R.id.three); 
      mFour = (Button) findViewById(R.id.four); 
      mFive = (Button) findViewById(R.id.five); 
      mSix = (Button) findViewById(R.id.six); 
      mSeven = (Button) findViewById(R.id.seven); 
      mEight = (Button) findViewById(R.id.eight); 
      mNine = (Button) findViewById(R.id.nine); 

      mOne.setText("1"); 
      mTwo.setText("2"); 
      mThree.setText("3"); 
      mFour.setText("4"); 
      mFive.setText("5"); 
      mSix.setText("6"); 
      mSeven.setText("7"); 
      mEight.setText("8"); 
      mNine.setText("9"); 

      mOne.setBackgroundResource(R.drawable.bottone_premuto); 
      mTwo.setBackgroundResource(R.drawable.bottone_premuto); 
      mThree.setBackgroundResource(R.drawable.bottone_premuto); 
       mFour.setBackgroundResource(R.drawable.bottone_premuto); 
      mFive.setBackgroundResource(R.drawable.bottone_premuto); 
      mSix.setBackgroundResource(R.drawable.bottone_premuto); 
      mSeven.setBackgroundResource(R.drawable.bottone_premuto); 
      mEight.setBackgroundResource(R.drawable.bottone_premuto); 
      mNine.setBackgroundResource(R.drawable.bottone_premuto); 

} 


} 

現在我想的是改變按鈕的顏色由btn_defa按下確定當按下ult_pressed_holo_dark時,我需要在我的MainActivity.java中添加哪些代碼? 在此先感謝,對於長篇帖子感到抱歉。 喜尼古拉

+0

Google it。你可以找到很好的教程 – Rick

+0

@ user2675569你是對的,但 是我正在尋找的一個月,我發現沒有什麼可以解決我的問題 – Nicola

+0

你可以在這裏找到很好的教程: http://developer.android.com/guide /topics/resources/drawable-resource.html#StateList http://stackoverflow.com/questions/4336060/android-how-to-add-a-custom-button-state http://stackoverflow.com/questions/8852420 /以狀態爲中心的狀態爲按鈕 – Rick

回答

0

我解決我的問題,刪除該代碼

mOne.setBackgroundResource(R.drawable.bottone_premuto); 
     mTwo.setBackgroundResource(R.drawable.bottone_premuto); 
     mThree.setBackgroundResource(R.drawable.bottone_premuto); 
      mFour.setBackgroundResource(R.drawable.bottone_premuto); 
     mFive.setBackgroundResource(R.drawable.bottone_premuto); 
     mSix.setBackgroundResource(R.drawable.bottone_premuto); 
     mSeven.setBackgroundResource(R.drawable.bottone_premuto); 
     mEight.setBackgroundResource(R.drawable.bottone_premuto); 
     mNine.setBackgroundResource(R.drawable.bottone_premuto); 

,並添加該代碼

mOne.setOnTouchListener(new View.OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      if (event.getAction() == MotionEvent.ACTION_DOWN) { 
       mOne.setBackgroundColor(Color.argb(33, 33, 44, 255)); 
       return true; 
      } else if (event.getAction() == MotionEvent.ACTION_UP) { 
       mOne.setBackgroundResource(R.drawable.btn_default_normal_holo_dark); 
       return true; 
      } 
      return false; 
     } 
    }); 
    mTwo.setOnTouchListener(new View.OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      if (event.getAction() == MotionEvent.ACTION_DOWN) { 
       mTwo.setBackgroundColor(Color.argb(33, 33, 44, 255)); 
       return true; 
      } else if (event.getAction() == MotionEvent.ACTION_UP) { 
       mTwo.setBackgroundResource(R.drawable.btn_default_normal_holo_dark); 
       return true; 
      } 
      return false; 
     } 
    }); 
    mThree.setOnTouchListener(new View.OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      if (event.getAction() == MotionEvent.ACTION_DOWN) { 
       mThree.setBackgroundColor(Color.argb(33, 33, 44, 255)); 
       return true; 
      } else if (event.getAction() == MotionEvent.ACTION_UP) { 
       mThree.setBackgroundResource(R.drawable.btn_default_normal_holo_dark); 
       return true; 
      } 
      return false; 
     } 
    }); 
    mFour.setOnTouchListener(new View.OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      if (event.getAction() == MotionEvent.ACTION_DOWN) { 
       mFour.setBackgroundColor(Color.argb(33, 33, 44, 255)); 
       return true; 
      } else if (event.getAction() == MotionEvent.ACTION_UP) { 
       mFour.setBackgroundResource(R.drawable.btn_default_normal_holo_dark); 
       return true; 
      } 
      return false; 
     } 
    }); 
    mFive.setOnTouchListener(new View.OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      if (event.getAction() == MotionEvent.ACTION_DOWN) { 
       mFive.setBackgroundColor(Color.argb(33, 33, 44, 255)); 
       return true; 
      } else if (event.getAction() == MotionEvent.ACTION_UP) { 
       mFive.setBackgroundResource(R.drawable.btn_default_normal_holo_dark); 
       return true; 
      } 
      return false; 
     } 
    }); 
    mSix.setOnTouchListener(new View.OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      if (event.getAction() == MotionEvent.ACTION_DOWN) { 
       mSix.setBackgroundColor(Color.argb(33, 33, 44, 255)); 
       return true; 
      } else if (event.getAction() == MotionEvent.ACTION_UP) { 
       mSix.setBackgroundResource(R.drawable.btn_default_normal_holo_dark); 
       return true; 
      } 
      return false; 
     } 
    }); 
    mSeven.setOnTouchListener(new View.OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      if (event.getAction() == MotionEvent.ACTION_DOWN) { 
       mSeven.setBackgroundColor(Color.argb(33, 33, 44, 255)); 
       return true; 
      } else if (event.getAction() == MotionEvent.ACTION_UP) { 
       mSeven.setBackgroundResource(R.drawable.btn_default_normal_holo_dark); 
       return true; 
      } 
      return false; 
     } 
    }); 
    mEight.setOnTouchListener(new View.OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      if (event.getAction() == MotionEvent.ACTION_DOWN) { 
       mEight.setBackgroundColor(Color.argb(33, 33, 44, 255)); 
       return true; 
      } else if (event.getAction() == MotionEvent.ACTION_UP) { 
       mEight.setBackgroundResource(R.drawable.btn_default_normal_holo_dark); 
       return true; 
      } 
      return false; 
     } 
    }); 
    mNine.setOnTouchListener(new View.OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      if (event.getAction() == MotionEvent.ACTION_DOWN) { 
       mNine.setBackgroundColor(Color.argb(33, 33, 44, 255)); 
       return true; 
      } else if (event.getAction() == MotionEvent.ACTION_UP) { 
       mNine.setBackgroundResource(R.drawable.btn_default_normal_holo_dark); 
       return true; 
      } 
      return false; 
     } 
    }); 

,但現在我希望把它在沒有onTouchListener另一種方式,將安卓的onClick =「button_pressed」給我的.xml中的每個按鈕,並用這種新方法調用它

public void button_pressed(View v) { 

    button_pressed = findViewById(R.drawable.btn_default_pressed_holo_dark); 
    button_pressed.setBackgroundColor(Color.argb(33, 243, 55, 123)); 

} 

但按下按鈕時總是顯示由btn_default_pressed_holo_dark定義的顏色。 我做錯了什麼?