在Android模拟器上,我的按钮没有单击

时间:2014-03-16 09:01:44

标签: java android android-layout button android-framelayout

我最近开始编程并遇到了这个问题。我的按钮没有点击模拟器,但是,我在java中写了onClickListener。它仍然无法运作。

这是我的xml代码:

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

<Button
android:id="@+id/btn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/SD"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="70dp"
android:background="@color/blue_gray"
android:visibility="visible"
android:onClick="onClick"/>
<Button
    android:id="@+id/btn2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/SD2"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="70dp"
    android:background="@color/follow"
    android:visibility="gone"
    android:onClick="onClick"/>
</FrameLayout>

这是我的java代码:

enter code here                包com;

           import android.app.Activity;
           import android.os.Bundle;
           import android.view.View;
           import android.widget.Button;
           import android.widget.ListView;
           import android.widget.TextView;
           import com.example.app.R;

           /**
           * Created by ww on 12.02.14.
           */



           public class fragment_main extends Activity  {

           Button i1;
           Button i2;


            protected void onCreate (Bundle SavedInstanceState){
            super.onCreate(SavedInstanceState);
            setContentView(R.layout.fragment_main);

             i1= (Button) findViewById(R.id.btn1);
             i2=(Button) findViewById(R.id.btn2);



             }




             public void onClick(View view) {
             switch (view.getId()) {
             case R.id.btn1:
             i1.setVisibility(View.GONE);
             i2.setVisibility(View.VISIBLE);
             break;
             case R.id.btn2:
             i1.setVisibility(View.VISIBLE);
             i2.setVisibility(View.GONE);

             break;
              }
              }
              }

2 个答案:

答案 0 :(得分:0)

你必须在你的按钮上注册一个监听器,例如:

i1 = (Button) findViewById(R.id.btn1);
i2 = (Button) findViewById(R.id.btn2);
//inside onCreate do this:
i1.setOnClickListener(myhandler1);
i2.setOnClickListener(myhandler2);

然后你必须创建那些听众。

// somewhere outside onCreate do this:
View.OnClickListener myhandler1 = new View.OnClickListener() {
public void onClick(View v) {
  // it was the 1st button
}
};
View.OnClickListener myhandler2 = new View.OnClickListener() {
public void onClick(View v) {
  // it was the 2nd button
}
};

答案 1 :(得分:0)

这是工作代码,

activity_main.xml中

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

<Button
android:id="@+id/btn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/sd"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="70dp"
android:onClick="onClick"/>

<Button
android:id="@+id/btn2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/sd2"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="140dp"
android:visibility="gone"
android:onClick="onClick"/>    

</RelativeLayout>

MainActivity.java

package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity  {

    Button b1;
    Button b2; 

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         b1 = (Button) findViewById(R.id.btn1);
         b2 = (Button) findViewById(R.id.btn2);
    }


    public void onClick(View v) {
        switch(v.getId()) {
            case R.id.btn1:
                b2.setVisibility(View.VISIBLE);
                b1.setVisibility(View.INVISIBLE);
           break;
            case R.id.btn2:
                b2.setVisibility(View.INVISIBLE);
                b1.setVisibility(View.VISIBLE);
           break;

        }
     }
  }


输出(在模拟器中编译和运行):     
    

  • 应用程序启动时Button2将不可见,因为它在xml layout中设置为不可见。

    enter image description here

    • 用户点击上方的button1后,button2将会显示&amp; button1将不可见。



    enter image description here