2012-03-27 66 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; 
    } 
}