【问题标题】:how to add onlclick listener android如何添加onclick监听器android
【发布时间】:2013-10-09 11:05:15
【问题描述】:

好的,我的 android 应用程序主页上有多个按钮。

在编写这样的应用程序时,我似乎一直感到困惑。

protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public void aboutus (final View view) {
    setContentView(R.layout.aboutus);
    System.out.println("about us clicked"); 
}

我已经在 xml 中添加了 on click。

但是,我需要在“关于我们”页面中编写代码。

在关于我们的课程中,我有以下内容

public class AboutUs extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.aboutus);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    public void onBackPressed() {
        setContentView(R.layout.activity_main);
    }
}

这里是被点击按钮的xml代码

<ImageButton
    android:id="@+id/ImageButton01"
    android:layout_width="134dp"
    android:layout_height="97dp"
    android:layout_x="16dp"
    android:layout_y="150dp"
    android:onClick="aboutus"
    android:src="@drawable/aboutus" />

我正在编写的任何代码在该页面上都不起作用?我不知道为什么?有人可以帮忙吗?

谢谢。

【问题讨论】:

  • 我不清楚你的问题,你在 xml 中添加 onClick 吗?那么当你点击那个按钮时会发生什么?
  • 抱歉,当我单击按钮时,会加载正确的 xml 页面。但是按钮单击类中的所有代码都不起作用? @Monica Ive 发布了我的 xml
  • 好吧,这不是你在android中改变视图的方式,如果你想导航到另一个视图,你需要启动另一个Activity,你会这样做。 How to navigate from one screen to another screen 和博客条目 Android Activity – From One Screen To Another Screen
  • 你需要改变你的代码结构。
  • NARESH REDDY 是正确的检查他的答案,并在清单中添加你的其他类

标签: android xml class


【解决方案1】:

像这样更改您的MainActivity

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void aboutus(final View view)
    {
        startActivity(new Intent(MainActivity.this, AboutUs.class));
    }

AboutUs.java 就像:

public class AboutUs extends Activity{
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.aboutus);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    /* No need of this method */
    public void onBackPressed() {
          setContentView(R.layout.activity_main);
    }
}

注意:

android:onClick 适用于 API 级别 4 及更高版本,因此如果您以 为目标,则无法使用它。

【讨论】:

  • 它给了我一个错误,说我没有在 android manifest xml 中声明活动?
  • 那么你必须在AndroidManifest.xml中定义Activity
【解决方案2】:

您不能只在 aboutus 方法中调用 setContentView,您需要创建一个启动新活动的意图。调用setContentView 只是在当前主要活动中设置视图,并不会实例化您的AboutUs 活动类。您的 aboutus 方法应类似于:

public void aboutus (final View view)
{   
    Intent intent = new Intent(this, AboutUs.class);
    startActivity(intent);
}

【讨论】:

    【解决方案3】:

    我认为您正在使用 setContentView() 呼叫另一个 Activity。 你必须使用意图

    Intent in = new Intent(this,AboutUs.class);
    startActivity(in);
    

    【讨论】:

    • 你能再解释一下,让我明白吗?
    • 当您使用 setContentView 时,您将处于同一个活动中,因此您在第二个活动中处理的点击在这里不起作用,因此当您想更改为另一个活动时,请使用 Intents
    猜你喜欢
    • 2018-08-30
    • 2014-06-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-31
    • 1970-01-01
    • 2020-07-27
    • 2015-04-19
    • 1970-01-01
    相关资源
    最近更新 更多