2011-03-26 111 views
9

我的所有其他onClick方法工作,除了我必须膨胀的布局,以获得按钮!我究竟做错了什么!下面是一些代码:膨胀的布局的按钮onClick监听器不工作

package com.games.think; 

import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 

import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 

import android.widget.Button; 
import android.widget.RadioButton; 
import android.widget.TableLayout; 

public class Think extends Activity{ 
    //What question are we on? 
    int question = 1; 
    //What level are we on? 
    String lvl ="1"; 

    //Radio buttons we need to access them global 
    RadioButton lvl1; 
    RadioButton lvl2; 
    RadioButton lvl3; 
    RadioButton lvl4; 
    RadioButton lvl5; 

    /** Called when the activity is first created. */ 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     //play 
     Button play = (Button)findViewById(R.id.play); 
     play.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       playOnClick(v); 

      } 
     }); 
     //level 
     Button level = (Button)findViewById(R.id.level); 
     level.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       levelOnClick(v); 

      } 
     }); 

     //setLevel 
     LayoutInflater inflater = LayoutInflater.from(this); 
     TableLayout tbl = new TableLayout(this); 

     View playv = inflater.inflate(R.layout.level, null); 
     Button updateLevel = (Button) playv.findViewById(R.id.updateLevel); 
     tbl.addView(updateLevel); 

     updateLevel.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        updateLevelOnClick(v); 
       } 
      }); 

     View levelv = inflater.inflate(R.layout.play, null); 
     Button gotomenu = (Button) levelv.findViewById(R.id.tomenu); 

     gotomenu.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        toMenuOnClick(v); 
       } 
      }); 





     //Radio Buttons 
     lvl1 = (RadioButton) levelv.findViewById(R.id.lvl1); 

     lvl2 = (RadioButton) levelv.findViewById(R.id.lvl2); 

     lvl3 = (RadioButton) levelv.findViewById(R.id.lvl3); 

     lvl4 = (RadioButton) levelv.findViewById(R.id.lvl4); 

     lvl5 = (RadioButton) levelv.findViewById(R.id.lvl5); 
     //tomenu 


     lvl = getLevel(); 


     if(lvl.equals("-1")) { 
      lvl=getLevel(); 

     } 






    } 




    protected void toMenuOnClick(View v) { 
     setContentView(R.layout.main); 

    } 




    protected void updateLevelOnClick(View v) { 

     setContentView(R.layout.main); 


    } 




    protected void levelOnClick(View v) { 
     setContentView(R.layout.level); 


     if(lvl.equals("1")) { 
      lvl1.setChecked(true); 
     } 
     if(lvl.equals("2")) { 
      lvl2.setChecked(true); 
     } 
     if(lvl.equals("3")) { 
      lvl3.setChecked(true); 
     } 
     if(lvl.equals("4")) { 
      lvl4.setChecked(true); 
     } 
     if(lvl.equals("5")) { 
      lvl5.setChecked(true); 
     } 

    } 




    protected void playOnClick(View v) { 
     setContentView(R.layout.play); 


     setQuestion(); 


    } 






    private String getLevel() { 

     String FILENAME = "think_level"; 
     FileInputStream fis; 
     byte[] buffer = new byte[1000]; 


     try { 
      fis = openFileInput(FILENAME); 
     } catch (FileNotFoundException e) { 
      setLevel("1"); 
      return "-1"; 
     } 

     try { 
      fis.read(buffer,0,1000); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     try { 
      fis.close(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     String level = buffer.toString(); 

     return level; 
    } 

    private void setLevel(String _level) { 
     String FILENAME = "think_level"; 
     String level = _level; 

     FileOutputStream fos; 
     try { 
      fos = openFileOutput(FILENAME, Context.MODE_PRIVATE); 
      fos.write(level.getBytes()); 
      fos.close(); 
     } catch (Exception e) { 

      e.printStackTrace(); 

     } 

    } 

    private void setQuestion() { 




    } 



    } 

这里是我的xml文件: 的main.xml:

<?xml version="1.0" encoding="utf-8"?> 

    <TableLayout android:id="@+id/tableLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> 
     <ImageView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/imageView1" android:src="@drawable/think" android:scaleType="fitCenter"></ImageView> 
     <Button android:id="@+id/play" android:text="Play" android:layout_height="wrap_content" android:layout_width="wrap_content" ></Button> 
     <Button android:text="Level" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/level"></Button> 


    </TableLayout> 

这里是我的level.xml

<?xml version="1.0" encoding="utf-8"?> 
    <TableLayout android:id="@+id/tableLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> 
     <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content"> 
      <TextView android:text="Level:" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> 
     </TableRow> 
     <TableRow android:id="@+id/tableRow2" android:layout_width="wrap_content" android:layout_height="wrap_content"> 
      <RadioGroup android:id="@+id/radioGroup1" android:layout_width="wrap_content" android:layout_height="wrap_content"> 
       <RadioButton android:id="@+id/lvl1" android:text="Level 1" android:layout_height="wrap_content" android:checked="true" android:layout_width="wrap_content"></RadioButton> 
       <RadioButton android:id="@+id/lvl2" android:text="Level 2" android:layout_height="wrap_content" android:layout_width="wrap_content"></RadioButton> 
       <RadioButton android:id="@+id/lvl3" android:text="Level 3" android:layout_height="wrap_content" android:layout_width="wrap_content"></RadioButton> 
       <RadioButton android:id="@+id/lvl4" android:text="Level 4" android:layout_height="wrap_content" android:layout_width="wrap_content"></RadioButton> 
       <RadioButton android:id="@+id/lvl5" android:text="Level 5" android:layout_height="wrap_content" android:layout_width="wrap_content"></RadioButton> 
      </RadioGroup> 
     </TableRow> 
     <Button android:text="Set Level" android:id="@+id/updateLevel" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 
    </TableLayout> 

这里是play.xml:

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <ScrollView android:id="@+id/scrollView1" android:layout_width="wrap_content" android:layout_height="wrap_content"> 
     <TextView android:text="TextView" android:id="@+id/question" android:layout_height="wrap_content" android:layout_width="wrap_content"></TextView> 
    </ScrollView> 
    <RadioGroup android:id="@+id/radioGroup1" android:layout_width="wrap_content" android:layout_height="wrap_content" > 
     <RadioButton android:id="@+id/radioButton1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="RadioButton"></RadioButton> 
     <RadioButton android:id="@+id/radioButton2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="RadioButton"></RadioButton> 
     <RadioButton android:id="@+id/radioButton3" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="RadioButton"></RadioButton> 
    </RadioGroup> 
    <Button android:id="@+id/go" android:text="Go" android:layout_height="wrap_content" android:layout_width="wrap_content"></Button> 
    <Button android:id="@+id/tomenu" android:text="Back To Menu" android:layout_height="wrap_content" android:layout_width="wrap_content"></Button> 
</TableLayout> 
+1

。您确定要添加的按钮在屏幕上? – 2011-03-28 23:56:15

+0

它们不在main.xml布局上,而是在其他布局上。是的,我确定它们在屏幕上,因为我可以进入level.xml和play.xml屏幕,但不能返回,因为onclick监听器不工作。 – 2011-03-29 00:10:14

回答

19

问题是您添加的视图没有父级,因此无法接收点击事件。目前您正在使用视图充气视图

View playv = inflater.inflate(R.layout.level, null); 

您需要在第二个参数中放置父视图。

View playv = inflater.inflate(R.layout.level, parentView, false); 

这将允许您使用父视图,但不实际附加它。

+0

我应该从level.xml文件中获取主视图组并添加主视图组作为第二个参数吗? – 2011-03-30 16:37:11

+0

我更新了我的问题 – 2011-03-30 19:25:55

+0

您将使用任何想要添加充气视图的视图。 – 2011-03-31 18:42:59

0

没有苹果为什么这不应该工作。使用

任何运行时创建的视图必须添加到您的LinearLayout(或任何其他家长布局) :

linearLayoutMain.addView(updateLevel); 

你有没有使用任何的线性布局的这个按钮,在XML文件中IE浏览器这些按钮当你达到相应的活动时出现?

+0

另外我没有得到你的意思,“我可以进入level.xml和play.xml屏幕”。我以为你正在使用这些xml来夸大按钮。那么你是否也在其他任何活动中使用这些? – 2011-03-29 09:24:56

+0

布局level.xml和play.xml实际上是具有父级按钮和图像的布局。 – 2011-03-30 00:16:00

1

所以这些按钮位于您的关卡和播放布局中,并且您在扩充这些布局,但看起来您并未将布局添加到主视图/布局。你真的能够看到你正在膨胀的这两种布局吗?

+0

在我的main.xml布局中,我有两个按钮玩和水平!当我点击播放时,它将我带到play.xml,我看到一切都很好,并且与关卡按钮相同。但是onclick监听器不工作,我该如何解决这个问题? – 2011-03-30 19:46:30

+0

好的...你确定这不是setLevelOnClick和toMenuOnClick函数的问题吗?你可以发布他们的代码吗? – BigFwoosh 2011-03-30 19:54:00

+0

发布了整个java文件 – 2011-03-30 20:01:04

0

您应该在tbl.addView(updateLevel);看到异常,因为您正试图添加一个父视图已经指定的视图。

你没看到这个例外吗?

+0

不,我没有看到异常 – 2011-04-03 18:16:15

+0

请检查您的发布代码,我使用了相同的代码,我看到这个例外。 – Karan 2011-04-04 03:26:01

0

通胀异步方法,你可以尝试后添加点击收听它的膨胀

//only inflate in oncreate/onresume 
    inflater.inflate(
      R.layout.play, // resource 
      this,    // root 
      true); //attach to root 

//overide onFinishInflate and get the view here 
    protected void onFinishInflate() { 
    super.onFinishInflate(); 
    Button gotomenu = (Button) levelv.findViewById(R.id.tomenu); 

    gotomenu.setOnClickListener(new View.OnClickListener() { 
     @Override 
      public void onClick(View v) { 
      toMenuOnClick(v); 
      } 
     }); 
    } 
0

如果您想对项目点击项目值只使用.setTag(某个值);getTag();

RelativeLayout firstContactRlayout = (RelativeLayout) myLayout.findViewById(R.id.firstContactRlayout); 
    firstContactRlayout.setTag(number); 
    firstContactRlayout.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      String gettags = (String) v.getTag(); 
      Log.e("gettags", "--" + gettags); 
     } 
    }); 
0

tbl.getChildAt(0)tbl.addView(updateLevel); 0可以改变试试这个:

tbl.getChildAt(0).findViewById(R.id.updateLevel).setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     updateLevelOnClick(v); 
    } 
});