Android Intents OnClickListener只有1个工作

时间:2017-01-15 18:39:50

标签: android android-intent onclicklistener

只有Numbers正在运行,其余的没有动作,但没有错误。当我点击Numbers Textview时它会工作并转到另一个意图,但是当我点击任何其他TextView时它根本没有反应。

所有活动都在清单中声明。

/*
 * 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);
            }
        });



    }


}

4 个答案:

答案 0 :(得分:1)

你已宣布

setContentView(R.layout.activity_main)

太多次了。在super.onCreate之后只声明一次。

答案 1 :(得分:1)

你应该只使用setContentView()一次。保留第一个并删除其他人,一切都应该正常工作。

答案 2 :(得分:1)

您正在拨打setContentView()两次。第二次创建一组新的View个对象,这些对象与您已设置OnClickListener的对象不同。新的View(实际显示的那些)没有任何监听器。删除第二个电话,你会很高兴。

P.S。当您有重复的代码时,您应该创建一个方法而不是简单地复制和粘贴。复制不同的地方是方法参数的良好候选者。在这种情况下,这样的事情会很好:

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);
            }
        });
}

现在,您可以使用一行创建每个侦听器:

createOnClickListener(colors, ColorsActivity.class);

答案 3 :(得分:0)

删除此行代码第二次设置布局会导致此问题。

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