2012-03-27 37 views
0

解決 - 改變layout_height在內部佈局WRAP_CONTENT幫助在嵌入式LinearLayout中使用的onClick

我開始使用Android開發昨天玩,我有一個關於嵌入佈局相當基本的問題。當我將一個LinearLayout嵌入到另一箇中並在其中放置一個Button時,onClick方法不會被調用。如果我省略第二個LinearLayout,則一切正常。你有什麼想法我做錯了什麼?

我的佈局文件如下。

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

     <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/prompt" 
     /> 

     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="horizontal" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
     > 
       <EditText 
         android:id="@+id/frequency" 
         android:layout_width="fill_parent" 
         android:layout_height="wrap_content" 
         android:layout_weight="1" 
         android:inputType="numberDecimal" 
       /> 

       <Button 
         android:id="@+id/submit" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="@string/submit" 
         android:onClick="handleSubmit" 
       /> 
     </LinearLayout> 

     <TextView 
       android:id="@+id/result" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
     /> 
</LinearLayout> 

,代碼:

package pl.test.android.step; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.inputmethod.EditorInfo; 
import android.view.KeyEvent; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Button; 

public class Step extends Activity 
{ 
     /** Called when the activity is first created. */ 
     @Override 
     public void onCreate(Bundle savedInstanceState) 
     { 
       super.onCreate(savedInstanceState); 
       setContentView(R.layout.main); 
     } 

     public void handleSubmit(View view) 
     { 
       TextView resultObj = (TextView) findViewById(R.id.result); 
       resultObj.setText("click"); 
     } 
} 

回答

0

您使用android:layout_height="fill_parent"你的第二個linearLayour,只是將其更改爲android:layout_height="wrap_content"。它會正常工作。

第二個LinearLayout完全填充了android頁面(主LinearLayout),並且「result textView」超出頁面。代碼正常工作,但你看不到你的textView。

+0

現在,它的偉大工程。謝謝! – Fraxion 2012-03-27 09:42:49

+1

關於爲什麼這是一個問題的任何解釋? – 2012-03-27 12:41:41

+0

@Sebastien:對不起。我的回答編輯 – AliSh 2012-03-27 12:48:37

1

嘗試:

public void handleSubmit(View view) 
{ 
    switch(view.getId()) 
    { 
     case R.id.result: 
     //Your method 
     break; 
    } 
}