gpt4 book ai didi

android - 如何从主要 Activity 开始新 Activity 并显示所有元素以及背景?

转载 作者:行者123 更新时间:2023-11-29 14:49:15 25 4
gpt4 key购买 nike

我是 Android 编程的初学者,为了学习它,我正在使用网络上的引用资料自己制作一个示例计算器示例,现在这是我想做的事情。我遵循了 Google 官方文档中提供的教程“构建您的第一个应用程序”,并且成功地引发了使用 Intents 类点击按钮的新 Activity ,这是该应用程序的图像开始 -: enter image description here

但是当我按下回车按钮时,即使我为新 Activity 设置了 android:background 选项,它也没有将背景设置为该图像,我唯一得到的是一个空白 Activity ,这是图像-:

enter image description here

这是在应用程序启动时启动的 MainActivity 的代码 -

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/math_symbols" >

<Button android:id="@+id/enter_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/enter_button"
android:onClick="enterCalculator"/>
</LinearLayout>

这是第二个 Activity 的代码,当我按下回车按钮时开始,这是使用 Intents 完成的:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/calculato_activity_1" >

<RadioGroup android:id="@+id/operation_radio_group"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dip"
android:orientation="horizontal">

<RadioButton android:id="@+id/sum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sum">

</RadioButton>

<RadioButton android:id="@+id/difference"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/difference">

</RadioButton>

<RadioButton android:id="@+id/multiply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/multiply">

</RadioButton>

</RadioGroup>
</LinearLayout>

上面没有显示背景,也没有向屏幕添加 View ,这也是我在 MainActivity 类中使​​用 Intents 的方式 -:

package com.example.mathcalculator;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;

public class MainActivity extends ActionBarActivity {

// this is the message used to enter the main calculator activity
public static String ENTER_MESSAGE = "com.example.mathcalculator.MainActivity.Message";

// this method is used to enter the actual calculator screen
public void enterCalculator(View view){

// we create an Intent to carry over the data from the button and enter the CalculatorActivity1
Intent intent = new Intent(this, CalculatorActivity1.class);

// put the key-value pair into the intent
intent.putExtra(ENTER_MESSAGE, "ENTER");

// start the new activity with the intent as the input which carries over the message bundle
startActivity(intent);
}

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

if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {

public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}

}

这是 CalculatorActivity1 类的代码 -:

package com.example.mathcalculator;

import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

public class CalculatorActivity1 extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.calculator_activity1, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {

public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(
R.layout.fragment_calculator_activity1, container, false);
return rootView;
}
}

}

最佳答案

您需要为您的 Activity 设置内容 View 。它已经在示例应用 Activity 中完成,因此在 MainActivity 的 oncreate 方法中,您可以看到:setContentView(R.layout.activity_main);它将 activity_main 布局设置为 MainActivity 内容 View 。因此,您也需要在 super.onCreate(savedInstanceState); 下对 CalculatorActivity1 执行此操作您可以在 res/layout 文件夹中找到您的第二个布局名称。

关于android - 如何从主要 Activity 开始新 Activity 并显示所有元素以及背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24024695/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com