Android Intents OnClickListener仅1种有效

沙尔托

只有“数字”有效,其余没有任何动作,但没有错误。当我单击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);
            }
        });



    }


}
学徒

您打了setContentView()两次电话第二次创建一组新View对象,这些对象与您已OnClickListener为其设置的对象不同View实际显示的是新的,没有任何侦听器。删除第二个电话,您将很方便。

ps如果有重复的代码,则应创建一个方法,而不是简单地复制粘贴。复制不同的位置是方法参数的理想候选者。在这种情况下,类似这样的方法会很好地工作:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用Intents Android Studio

android intents:检索意图参数

使用Intents Android打开新页面

如何通过Android Intents传递数组的值?

Android Intents,如何传递和接收一个InetAddress?

Android Blog App,我应该使用Sharedpreferences还是Intents?

startActivityForResults时,Android Espresso-Intents无法在Fragment中使用

Intent Intents.Insert.ACTION仅在第一次使用时有效?

Discord Intents 代码,用于从网关获取所有事件

Android使用Intents.Insert插入联系人不起作用

Android 10-使用PackageManager安装新版本的apk而不是Intents确实不起作用

无法解决:com.android.support.test.espresso:espresso-intents:27.0.2

[Android] [Tab-Intent]如何从Intents打开Tabactivity中的特定标签?

我需要一种有效的方法来复制Android Studio中的现有活动

ExpectedInputs / Possible_intents仅适用于“ assistant.intent.action.TEXT”吗?

Android:“ slideEdge”不再有效

Android检查是有效的UUID

如何有效学习android开发?

CLIENT_MISSING_INTENTS'

gcm.notification.e = 1在Android的推送通知有效载荷中代表什么?

Android jar 使用有效的密钥库未签名,需要 SHA1 证书吗?

没有有效的Android SDK平台

消息有效负载包含无效的“ android”属性。有效属性是“数据”和“通知”

Android gcm通知有效负载与数据有效负载?

iOS Siri Intents Extension“我没有看到用于此目的的应用程序。您需要下载一个。”

Discord.Intents(32767) vs [Intents.FLAGS.GUILDS]?

Android单播UDP有效,但广播不起作用

如何确定GPS坐标何时在Android中有效

Android使用Java有效读取大文本