2012-04-04 19 views
6

私は、さまざまな状態でボタンの背景を使用する方法について説明したが、動作していないようチュートリアルを追ってきた。ここでSプレスアンドロイドボタンの状態

は私のコードです:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@drawable/boutonn" android:state_window_focused="false"/> 
    <item android:drawable="@drawable/boutonnpousse" android:state_pressed="true"/> 
    <item android:drawable="@drawable/boutonnpousse" android:state_focused="true"/> 
    <item android:drawable="@drawable/boutonn" android:state_focused="false" 
    android:state_pressed="false" /> 

</selector> 

これはここで、私は私の描画可能なフォルダに配置したXMLコードでこれらのボタンを使用して活動のXMLの一部です:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/backgrounddd" 
    android:orientation="vertical" > 

      <Button 
       android:id="@+id/bNoteRemind" 
       android:layout_width="100dp" 
       android:layout_height="100dp" 
       android:layout_gravity="center" 
       android:background="@drawable/imagebutton1" /> 
    ... 

そしてここでは、Javaクラスです:

public class MenuPrincipal extends Activity { 

    Button NoteRemind;   

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 

     //on lui associe le layout menuprincipal.xml 
     setContentView(R.layout.menuprincipal); 

     NoteRemind = (Button) findViewById(R.id.bNoteRemind);  

     // Si on choisit de rédiger une nouvelle task on va être rediriger sur l'activité NoteReminder 

     NoteRemind.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 
       // TODO Auto-generated method stub 

       //On créé l'Intent qui va nous permettre d'afficher l'autre Activity 
       //Mettez le nom de l'Activity dans la quelle vous êtes actuellement pour le premier parametre 
       v.setPressed(true); 

       Intent intent = new Intent(MenuPrincipal.this, NoteReminder.class); 
       //Intent intent = new Intent(MenuPrincipal.this, Teste2.class); 
       //On démarre l'autre Activity 
       startActivity(intent); 


      } 
     }); .... 

ボタンがうまく表示されますが、押すと押した画像が表示されません。■間違っていることを理解できません。

誰かがエラーをどこかで見ますか?


これらの行はどこに置く必要がありますか?私は、ボタンのXMLに入れまし

<Button 
     android:id="@+id/bNoteRemind" 
     android:layout_width="100dp" 
     android:layout_height="100dp" 
     android:layout_gravity="center" 
     android:background="@drawable/imagebutton1" 
     android:focusable="true" 
     android:focusableInTouchMode="true" /> 

しかし、今、私のボタンの背景は、私はそれを押さずに押された画像に変更:pと、それは

+2

試してボタンを設定した後 'アンドロイド:

は実際に、私はこのコードを示唆してフォーカス可能な' = "true" を 'とAndroid:focusableInTouchMode = true'を –

+0

[http://stackoverflow.com/questions//オブ・トグルボタン方法ツー変色11978880] [1] このください を参照してください[1]:http://stackoverflow.com/質問/ 1 1978880/to-to-the-toggle-buttonの変更方法 – haris

答えて

18

はあなたが表示されている唯一のものButtonです変更されません。あなたのActivityにありますか?もしそうなら、ウィンドウがロードされたときにフォーカスされ(selectorの3番目のアイテムがトリガーされます)、そこからナビゲートすることはできません。押されたときにのみ変更したい場合は、その3行目を削除してください。ウィンドウがフォーカスされていないときにボタンが押されることはないので、最初の行を削除します。

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@drawable/boutonnpousse" android:state_pressed="true"/> 
    <item android:drawable="@drawable/boutonn"/> 
</selector> 
+0

を押すとアニメーションの速度を変更できますか? – wutzebaer