【问题标题】:Android: OnClickListener() not Working for button and imageAndroid:OnClickListener() 不适用于按钮和图像
【发布时间】:2015-02-17 17:09:20
【问题描述】:

我是 android 新手,这是我的第一个应用程序。 我正在尝试使用按钮导航,但它不起作用。有人可以帮忙吗? 此外,单击图像也不起作用。 这是我的代码:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);


    btn = (Button)findViewById(R.id.btnNext);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, SecondActivity.class);
            startActivity(intent);

        }
    });

    txt = (ImageView)findViewById(R.id.imageView);

    txt.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            Intent sendIntent = new Intent();
            sendIntent.setAction(Intent.ACTION_SEND);
            sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
            sendIntent.setType("text/plain");
            startActivity(sendIntent);
        }
    });

    if (savedInstanceState == null) {
        // Add the fragment on initial activity setup
        mainFragment = new MainFragment();
        getSupportFragmentManager()
                .beginTransaction()
                .add(android.R.id.content, mainFragment)
                .commit();
    } else {
        // Or set the fragment from restored state info
        mainFragment = (MainFragment) getSupportFragmentManager()
                .findFragmentById(android.R.id.content);
    }


}

xml文件

<ImageView
    android:layout_width="100dp"
    android:layout_height="110dp"
    android:id="@+id/imageView"
    android:layout_gravity="center_vertical"
    android:src="@drawable/com_facebook_button_check_on"
    android:layout_weight="0.61"
    android:layout_marginBottom="161dp"
    android:layout_alignParentBottom="true"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:longClickable="true"
    android:nestedScrollingEnabled="true"
    android:clickable="true"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/btn_title"
    android:id="@+id/btnNext"
    android:layout_below="@+id/authButton"
    android:layout_alignParentStart="true" />

提前致谢。

【问题讨论】:

  • 您尝试findViewById 的视图是在活动的布局中还是在片段中?如果在片段中,您应该在那里设置您的onClickListeners。
  • 显示什么错误?放入logcat文件。
  • 我在活动课上使用这个。回家后会分享日志文件。

标签: android android-intent android-studio onclicklistener


【解决方案1】:

尝试添加:

txt = (ImageView)findViewById(R.id.imageView);
txt.setClickable(true);   //<----

看看图片 onClick 是否有效。

另外虽然添加onClickListeners完全没问题,还有另一种调用按钮和ImageViews方法的方式,

在 xml 中将其添加到 ImageView 或 Button:

android:onClick="launch";

然后你只需要创建类似的方法,

public void launch(){
   //some code here
}

我不知道设置onClickListeners是否比这个更好,但我一直觉得这种方式更方便。

【讨论】:

  • 这将是 public void launch(View view){...},但这应该可以工作。
猜你喜欢
  • 2019-08-16
  • 2013-12-06
  • 2020-08-31
  • 1970-01-01
  • 1970-01-01
  • 2013-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多