2013-09-30 28 views
0

因此,我刚开始使用Android开发套件,并学习如何创建按钮并使其工作。为了测试我所知道的我做了一个简单的应用程序,如果你点击右边的按钮显示“正确”,如果你点击左边的按钮,你会看到“白痴”。除了点击按钮显示相反信息的事实以外,所有的工作都是有效的。Android按钮与命令相反

这里是我的XML代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" > 

<TextView 
    android:id="@+id/textv1" 
    android:layout_width="149dp" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="57dp" 
    android:text="Click the Right Button" /> 

<Button 
    android:id="@+id/left" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/textv1" 
    android:layout_marginTop="29dp" 
    android:layout_toLeftOf="@+id/textv1" /> 

<Button 
    android:id="@+id/right" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBaseline="@+id/left" 
    android:layout_alignBottom="@+id/left" 
    android:layout_toRightOf="@+id/textv1" /> 

这里是我的Java代码:

package com.clicktherightbutton; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

public class MainActivity extends Activity { 

Button rightb; 
Button leftb; 
TextView Display; 

String yes = "Correct!!!"; 
String no = "Idiot!!!"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    rightb=(Button)findViewById(R.id.right); 
    leftb=(Button)findViewById(R.id.left); 
    Display=(TextView)findViewById(R.id.textv1); 

    rightb.setOnClickListener(new View.OnClickListener() 
    { 

     @Override 
     public void onClick(View v) 
     { 
      // TODO Auto-generated method stub 
      if(rightb.isPressed()) 
       Display.setText(yes); 
       rightb.setEnabled(false); 
       leftb.setEnabled(false); 
     } 
    }); 

    leftb.setOnClickListener(new View.OnClickListener() 
    { 

     @Override 
     public void onClick(View v) 
     { 
      // TODO Auto-generated method stub 
      if(leftb.isPressed()) 
       Display.setText(no); 
       rightb.setEnabled(false); 
       leftb.setEnabled(false); 

     } 
    }); 
} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

} 

我明白,如果我转 “左” 和 “右” 按钮会做就像我想要的那样,但我想知道为什么它与我所编码的相反。我的代码有问题吗?

谢谢

回答

-1

布局更改为:

<TextView 
    android:id="@+id/textv1" 
    android:layout_width="149dp" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="57dp" 
    android:text="Click the Right Button" /> 

<Button 
    android:id="@+id/left" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/textv1" 
    android:layout_marginTop="29dp" 
    android:layout_toLeftOf="@id/textv1" /> 

<Button 
    android:id="@+id/right" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBaseline="@id/left" 
    android:layout_alignBottom="@id/left" 
    android:layout_toRightOf="@id/textv1" /> 

只能申报一次@+id - 它创建一个变量。稍后使用@id来引用相同的变量。

+0

我试过你说的但我得到了同样的结果。谢谢你的提示。 –

+0

你确定按钮布置正确吗?你可以添加一个文本给他们检查哪个是哪个。 – Szymon

+0

我刚刚添加了文字,左边是左边和右边是正确的。当我在GUI上单击它时,它也显示正确的ID,这让我更加困惑于它们如何混淆。 –