2012-01-14 41 views
0

你好我嘗試在android中使用簡單的應用程序,但我有很多問題;我的第一個應用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> 

我的課

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); 
    } 

    } 

我已經在UI良好resultat但是當我點擊鏈接沒有事情發生?

回答

7

您在onCreate()中將文字設置爲「hh」,因此單擊它不會改變它。

此外,你打電話setContentView()兩次,所以第二次只是使你已經編碼的一切無效。

試試這個:

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"); 
     } 
    }); 
} 
相關問題