2012-11-05 33 views
1

我刚刚接触了android,并且刚刚了解了一些活动,并决定创建一些活动。Android用户界面问题,按钮选择

和我MainActivity.java是,

public class MainActivity extends Activity { 


@Override 
public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Button a = (Button) findViewById(R.id.button1); 
    a.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      startActivity(new Intent(MainActivity.this, WidgetIntro.class)); 

      Button b = (Button) findViewById(R.id.button2); 
      b.setOnClickListener(new OnClickListener() { 

       public void onClick(View v) { 
        // TODO Auto-generated method stub 
        startActivity(new Intent(MainActivity.this, Alarm.class)); 

        Button c = (Button) findViewById(R.id.button4); 
        c.setOnClickListener(new OnClickListener() { 

         public void onClick(View v) { 
          // TODO Auto-generated method stub 
          startActivity(new Intent(MainActivity.this, Sms.class)); 

          Button d = (Button) findViewById(R.id.button6); 
          d.setOnClickListener(new OnClickListener() { 

           public void onClick(View v) { 
            // TODO Auto-generated method stub 
            startActivity(new Intent(MainActivity.this, Call.class)); 

            Button e = (Button) findViewById(R.id.button3); 
            e.setOnClickListener(new OnClickListener() { 

             public void onClick(View v) { 
              // TODO Auto-generated method stub 
              startActivity(new Intent(MainActivity.this, Notes.class)); 

              Button f = (Button) findViewById(R.id.button5); 
              f.setOnClickListener(new OnClickListener() { 

               public void onClick(View v) { 
                // TODO Auto-generated method stub 
                startActivity(new Intent(MainActivity.this, Cbdata.class)); 

               } 
              }); 
             } 
            }); 

           } 
          }); 

         } 
        }); 
       } 
      }); 

虽然这没有任何错误,我已经创造了一些文字敬酒每一个按钮,当我在我的手机上安装我的申请,我能访问按钮只是按照我编号的顺序,即“位置小部件”按钮,我的按钮ID是按钮1,除了此按钮,我可以选择任何其他按钮,一旦我选择了此按钮,我只能选择按钮2但没有任何其他按钮,为什么会发生这种情况?

我activity_main.xml中码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" > 

<Button 
    android:id="@+id/button7" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_marginBottom="26dp" 
    android:text="About Us" /> 

<Button 
    android:id="@+id/button3" 
    android:layout_width="125dp" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/button7" 
    android:layout_alignParentRight="true" 
    android:layout_marginBottom="36dp" 
    android:text="Note Remainder" /> 

<Button 
    android:id="@+id/button2" 
    android:layout_width="125dp" 
    android:layout_height="wrap_content" 
    android:layout_alignBaseline="@+id/button3" 
    android:layout_alignBottom="@+id/button3" 
    android:layout_alignParentLeft="true" 
    android:text="Alarm" /> 

<Button 
    android:id="@+id/button4" 
    android:layout_width="125dp" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/button2" 
    android:layout_alignParentLeft="true" 
    android:layout_marginBottom="40dp" 
    android:text="SMS" /> 

<Button 
    android:id="@+id/button6" 
    android:layout_width="125dp" 
    android:layout_height="wrap_content" 
    android:layout_alignBaseline="@+id/button4" 
    android:layout_alignBottom="@+id/button4" 
    android:layout_alignParentRight="true" 
    android:text="Call" /> 

<Button 
    android:id="@+id/button1" 
    android:layout_width="125dp" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/button6" 
    android:layout_alignParentRight="true" 
    android:layout_marginBottom="37dp" 
    android:text="Location Widget" /> 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:text="Hello User, Welcome to Mobile location Info! You Choose from the following options :" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

<Button 
    android:id="@+id/button5" 
    android:layout_width="125dp" 
    android:layout_height="wrap_content" 
    android:layout_alignBottom="@+id/button1" 
    android:layout_alignParentLeft="true" 
    android:text="Save my locations" /> 

</RelativeLayout> 

     } 
    }); 
} 

} 
+1

这是一个很常见的问题.. – 2013-04-26 12:09:31

回答

0

您已经定义了上一个按钮的onclick监听器里的每个按钮的onClick侦听器。因此,当您单击上一个按钮时,第一个按钮之后的每个按钮只有一个分配的onClick侦听器。

相反,这样做:

Button a = (Button) findViewById(R.id.button1); 
a.setOnClickListener(new OnClickListener() { 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     startActivity(new Intent(MainActivity.this, WidgetIntro.class)); 
    } 
}); 

Button b = (Button) findViewById(R.id.button2); 
b.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      startActivity(new Intent(MainActivity.this, Alarm.class)); 
    } 
}); 

小费。一般来说,当你看到超过3级左右的大括号时,就像这样,那么你知道存在一个问题:

0

那么首先,如果问题只能访问按钮,但是你编号的顺序,我会看到这是因为你已经将每个按钮嵌入到另一个按钮中,为了纠正这个问题,让每个按钮监听器都是onCreate方法的主要成员方法,每个按钮监听器都有相同的UI访问权限。希望帮助:)