setOnClickListener无法正常工作

问题描述 投票:0回答:1

首先,我是android的新手。

我的申请中有三项活动。即MainActivity,TC and navigation_drawer。

用户首先输入MainActivity,提供其姓名,然后单击按钮输入TC(工作正常)。 TC内部有两个按钮“同意”,“不同意”,如下所示。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".TC">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/agree"
        android:text="Agree"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/not_agree"
        android:text="Do not Agree"
        android:layout_below="@id/agree"/>

</RelativeLayout>

现在,如果用户单击Agree,则应转到navigation_drawer,如果单击不同意,则应回到MainActivity。它的代码是:

public class TC extends AppCompatActivity {

    private Button Agree, NotAgree;

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

        Agree = (Button) findViewById(R.id.agree);
        NotAgree = (Button) findViewById(R.id.not_agree);

        Agree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(TC.this, "Agree", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(TC.this, navigation_drawer.class);
                startActivity(intent);

            }
        });

        NotAgree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(TC.this, "Not Agree", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(TC.this, MainActivity.class);
                startActivity(intent);
            }
        });
    }
}

添加Toast只是为了检查是否调用了正确的方法(它正在调用正确的方法)] >>

AndroidManifest.xml包含(我不确定这是否有帮助):

        <activity android:name=".TC" android:noHistory="true"/>
        <activity android:name=".navigation_drawer" />
        <activity
            android:name=".MainActivity"
            android:noHistory="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

问题是,由于某种原因,无论我单击哪个按钮,它都始终重定向到navigation_drawer

,对于这两个按钮,Toast消息也显示为[[按预期。我不知道我在这里想念什么,请提出解决方案。谢谢。

首先,我是android的新手。我的申请中有三项活动。即MainActivity,TC和navigation_drawer。用户首先输入MainActivity,前提是他/她的名字是...

android android-studio onclicklistener android-button buttonclick
1个回答
0
投票
您可以检查“ NotAgree”的初始化吗?我认为您初始化了NotAgree并同意。
© www.soinside.com 2019 - 2024. All rights reserved.