简体   繁体   English

Android Intents OnClickListener仅1种有效

[英]Android Intents OnClickListener only 1 works

Only Numbers is working, the rest has no action, but there is no errors. 只有“数字”有效,其余没有任何动作,但没有错误。 When i click on the Numbers Textview it work and go to the other intent, but when i click on any other TextView it doesn't react at all. 当我单击Numbers Textview时,它可以工作并转到其他意图,但是当我单击任何其他TextView时,它根本不起作用。

All activities are declared in the manifest. 所有活动都在清单中声明。

/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.example.android.miwok;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

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

        // Set the content of the activity to use the activity_main.xml layout file
        setContentView(R.layout.activity_main);

        // Find the View that shows the phrases category
        TextView colors = (TextView) findViewById(R.id.colors);

// Set a click listener on that View
        colors.setOnClickListener(new View.OnClickListener() {
            // The code in this method will be executed when the phrases View is clicked on.
            @Override
            public void onClick(View view) {
                Intent colorsIntent = new Intent(MainActivity.this, ColorsActivity.class);
                startActivity(colorsIntent);
            }
        });




        // Set the content of the activity to use the activity_main.xml layout file
        setContentView(R.layout.activity_main);

        // Find the View that shows the phrases category
        TextView family = (TextView) findViewById(R.id.family);

// Set a click listener on that View
        family.setOnClickListener(new View.OnClickListener() {
            // The code in this method will be executed when the phrases View is clicked on.
            @Override
            public void onClick(View view) {
                Intent familyIntent = new Intent(MainActivity.this, FamilyActivity.class);
                startActivity(familyIntent);
            }
        });




        // Set the content of the activity to use the activity_main.xml layout file
        setContentView(R.layout.activity_main);

        // Find the View that shows the phrases category
        TextView phrases = (TextView) findViewById(R.id.phrases);

// Set a click listener on that View
        phrases.setOnClickListener(new View.OnClickListener() {
            // The code in this method will be executed when the phrases View is clicked on.
            @Override
            public void onClick(View view) {
                Intent phrasesIntent = new Intent(MainActivity.this, PhrasesActivity.class);
                startActivity(phrasesIntent);
            }
        });


        // Set the content of the activity to use the activity_main.xml layout file
        setContentView(R.layout.activity_main);

        // Find the View that shows the phrases category
        TextView numbers = (TextView) findViewById(R.id.numbers);

// Set a click listener on that View
        numbers.setOnClickListener(new View.OnClickListener() {
            // The code in this method will be executed when the phrases View is clicked on.
            @Override
            public void onClick(View view) {
                Intent numbersIntent = new Intent(MainActivity.this, NumbersActivity.class);
                startActivity(numbersIntent);
            }
        });



    }


}

U have declared 你已经宣布

setContentView(R.layout.activity_main)

too many times. 太多次了 Declare it only once after super.onCreate. 在super.onCreate之后仅声明一次。

you should use setContentView() just once. 您应该只使用一次setContentView() keep the first one and remove others and everything should work just fine. 保留第一个,删除其他,一切都应该正常。

You are calling setContentView() twice. 您两次调用setContentView() The second time creates a new set of View objects which are different than the ones which you already set OnClickListener s for. 第二次创建新的View对象集,该对象集与您已OnClickListener设置OnClickListener的对象不同。 The new View , which are the ones actually displayed, do not have any listeners. 实际显示的新View没有任何侦听器。 Remove the second call and you will be good to go. 删除第二个电话,您将很方便。

ps When you have duplicate code, you should create a method rather than simply copy-and-paste. ps如果有重复的代码,则应创建一个方法,而不是简单地复制粘贴。 The places where the duplication differs are good candidates for method parameters. 复制不同的位置是方法参数的理想候选者。 In this case, something like this will work well: 在这种情况下,类似这样的方法会很好地起作用:

private void createOnClickListener(TextView view, Class<Activity> activityClass) {
    view.setOnClickListener(new View.OnClickListener() {
            // The code in this method will be executed when the phrases View is clicked on.
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, activityClass);
                startActivity(intent);
            }
        });
}

Now you can create each listener with a single line: 现在,您可以使用一行创建每个侦听器:

createOnClickListener(colors, ColorsActivity.class);

Remove this line of code It is setting the layout second time which is causing this issue. 删除此行代码这是第二次设置布局,从而导致此问题。

// Set the content of the activity to use the activity_main.xml layout file
 setContentView(R.layout.activity_main);

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

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