简体   繁体   English

OnClick不适用于LinearLayout

[英]OnClick doesn't work with LinearLayout

OnClick doesn't work. OnClick不起作用。 Nothing happens after clicking on layout. 单击布局后没有任何反应。 It seems like it is clickable, because layout changes its color, but new layout doesn't open. 似乎可以点击,因为布局会更改其颜色,但新的布局不会打开。

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/window"
     android:layout_width="295dp"
     android:layout_height="wrap_content"
     android:orientation="vertical" 
     android:background="@drawable/editborder"
     android:clickable="true"
     android:onClick="openBigImage">

Here is more code for Main Activity: 这是主要活动的更多代码:

 public class MyMapActivity extends FragmentActivity implements LocationListener
 {

     private Marker marker;
     private Hashtable<String, String> markers;
     private ImageLoader imageLoader;
     private DisplayImageOptions options;
     private GoogleMap map;
     private ListView mainListView ;  
     private ArrayAdapter<String> listAdapter ;




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

    // Look up the AdView as a resource and load a request.
    //AdView adView = (AdView)this.findViewById(R.id.adView);
    //adView.loadAd(new AdRequest());


    // Getting Google Play availability status
    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());

    // Showing status
    if(status!=ConnectionResult.SUCCESS)
    { // Google Play Services are not available
        int requestCode = 10;
        Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
        dialog.show();

    }
    else 
    {// Google Play Services are available
        // Getting reference to the SupportMapFragment of activity_main.xml
        SupportMapFragment mapFragment = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);

        if (savedInstanceState == null) {
            // First incarnation of this activity.
            mapFragment.setRetainInstance(true);
        } 
        else 
        {
            // Reincarnated activity. The obtained map is the same map instance in the previous
            // activity life cycle. There is no need to reinitialize it.
            map = mapFragment.getMap();
        }

        setUpMapIfNeeded();
    }
 }

 @Override
 protected void onResume() 
 {
      super.onResume();
      setUpMapIfNeeded();
 }


 public void openBigImage(View v)
     {
        setContentView(R.layout.bigpicture);
     }

bigpicture.xml: bigpicture.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"       
    android:id="@+id/bigpicture"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    android:orientation="vertical">

<fragment
    android:id="@+id/minimap"
    android:layout_width="200px"
    android:layout_height="200px"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true"
    class="com.google.android.gms.maps.SupportMapFragment" />

 <ImageView
    android:id="@+id/badge"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:adjustViewBounds="true" />
 </RelativeLayout> 

Calling setContentView() multiple times worked in other cases, like menu items "about", "settings" etc. Tried to make without setContentView. 在其他情况下,如菜单项“ about”,“ settings”等,多次调用setContentView()也是可行的。尝试不使用setContentView进行制作。 I've put new Layout to the main.xml and made visibility GONE. 我将新的Layout放在main.xml中,并使可见性消失了。 OnClick method should change visibility to visible, but again nothing happens. OnClick方法应将可见性更改为可见,但再次没有任何反应。 Logcat says "11-25 13:47:28.638: D/GestureDetector(3156): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 2 mFalseSizeCnt:0" when i'm clicking on linear layout. 当我单击线性布局时,Logcat会说“ 11-25 13:47:28.638:D / GestureDetector(3156):[表面触摸事件] mSweepDown False,mLRSDCnt:-1 mTouchCnt:2 mFalseSizeCnt:0”。

Paul, One thing is close the linear layout with /> .I am assuming that you have followed the map tutorials link and passed all the manifest permissions and other requirements. 保罗,一件事是使用/>封闭线性布局。我假设您已经遵循了地图教程链接并通过了所有清单权限和其他要求。 You might have some reasons to use px. 您可能有一些使用px的原因。 Check if map is being created. 检查是否正在创建地图。 Also give some height and background color to your badge image and see if something happens. 还给徽章图像提供一些高度和背景色,看看是否发生了什么。

I tested your code without map fragment and it worked fine. 我测试了没有地图片段的代码,并且效果很好。 Can you post the error log ? 您可以发布错误日志吗?

Found. 找到了。 It is a click on InfoWindow, so we should implement onInfoWindowClick. 单击InfoWindow,因此我们应该实现onInfoWindowClick。 But first we must add map.setOnInfoWindowClickListener(this); 但是首先我们必须添加map.setOnInfoWindowClickListener(this); in main activity. 在主要活动中。 Main activity must implement OnInfoWindowClickListener. 主要活动必须实现OnInfoWindowClickListener。 I've added new LinearLayout to the main.xml, made it invisible. 我在main.xml中添加了新的LinearLayout,使其不可见。 Here's code for onInfoWindowClick: 以下是onInfoWindowClick的代码:

 @Override
public void onInfoWindowClick(Marker marker) {
    LinearLayout secondLL = (LinearLayout) findViewById(R.id.bigpicture);
    int visibility = secondLL.getVisibility();
    if(visibility == View.GONE)
    {
         secondLL.setVisibility(View.VISIBLE);
    }

}

I think you can't use onClick attribute. 我认为您不能使用onClick属性。 You have to use setOnClickListener() like that : 您必须像这样使用setOnClickListener():

LinearLayout layout = (LinearLayout )findViewById(R.id.window);
layout .setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        YourActivity.this.setContentView(R.layout.bigpicture);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM