【问题标题】:i can't create a activity and link button to this activity [closed]我无法创建活动并将按钮链接到此活动[关闭]
【发布时间】:2015-08-27 07:23:54
【问题描述】:

我不能创建一个活动和链接到这个活动的按钮!! 例如,您可以制作一个项目并为我发送下载链接?? 这是essay,但我是新手 (安卓,食人鱼)

【问题讨论】:

  • 发布你所做的事情

标签: android button android-activity


【解决方案1】:

在 Manifest 文件中创建条目:

<activity android:label="@string/app_name" android:name="NextActivity"/>

关于你的活动(FirstActivity - 你有按钮的地方):

public class FirstActivity extends Activity {

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

    Button btn = (Button) findViewById(R.id.first_button_id_here);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(FirstActivity.this, NextActivity.class);
            startActivity(intent);
        }
    });
}
}

【讨论】:

    【解决方案2】:

    嗯,问题的解决方法很简单:

    0 假设您创建了两个活动:YourActivity 是第一个活动,您想转到 YourSecondActivity

    1 打开您的第一个活动的 XML 文件 (your_activity.xml)

    2 以这种方式添加要声明的按钮:

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_text"
        android:onClick="goToAnotherActivity" />
    

    3 打开活动的 Java 文件(类似于 YourActivity.java)

    4 声明函数:

    public void goToAnotherActivity(View view){
        Intent intent = new Intent(this, YourSecondActivity.class);
        startActivity(intent);
    }
    

    5 试试看。

    但是,我认为问题的解决方案是另一种方法:阅读一些初学者指南。我在这里学习了 Android 的官方指南https://developer.android.com/training/basics/firstapp/starting-activity.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-07
      • 1970-01-01
      相关资源
      最近更新 更多