2012-01-14 13 views
0

こんにちは私はアンドロイドで単純なアプリケーションを使用しようとするが、私は多くの問題があります。私の最初のアプリケーションandroid

"hh"でボタンのテキストをクリックすると必要です。

私のmain.xml

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


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

</LinearLayout> 

私のクラス私はボタンでクリックしたときに、私はUIでは良いresultatではなく、事が起きている

package com.my.Hello; 



import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 

public class HelloActivity extends Activity{ 
    Button button; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     button = (Button)findViewById(R.id.button1); 
     button.setText("hh"); 
     button.setOnClickListener(new View.OnClickListener() 
     { 
     public void onClick(View v) 
     { 
      button.setText("hh"); 
     } 
     }); 

     setContentView(R.layout.main); 
    } 

    } 

答えて

7

onCreate()にテキストを「hh」に設定しているので、クリックしても変更されません。

また、setContentView()を2回呼び出すので、2回目は既にコード化したものすべてを無効にします。

これを試してみてください:

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

    setContentView(R.layout.main); 

    button = (Button) findViewById(R.id.button1); 
    button.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      button.setText("hh"); 
     } 
    }); 
}