需要结果显示在新活动 [英] Need to display the result in new activity

查看:103
本文介绍了需要结果显示在新活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一个活动的结果显示在同一屏幕(窗口)(活动)。我只需要如下显示的结果在另一个activity.Can有人告诉我该怎么做that.The主要活动是:

 包com.example.knowyourself;

进口android.os.Bundle;
进口android.app.Activity;
进口android.view.View;
进口android.widget.Button;
进口android.widget.DatePicker;
进口android.widget.Toast;
进口android.view.View.OnClickListener;

公共类MainActivity扩展活动
{
按钮提交;
字符串温度;
DatePicker的DT;
INT日,月,年的选择;

@覆盖
保护无效的onCreate(包savedInstanceState)
{
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    addListenerOnButton();
}

公共无效addListenerOnButton()
{
    提交=(按钮)findViewById(R.id.button1);
    DT =(DatePicker的)findViewById(R.id.datePicker1);
    submit.setOnClickListener(新OnClickListener()
    {
        公共无效的onClick(视图v)
        {
            天= dt.getDayOfMonth();
            一个月= dt.getMonth();
            年= dt.getYear();
            选择=(日+月+年)5%;
            开关(选择)
            {
            ..............
                            ..............
            }
            吐司T = Toast.makeText(MainActivity.this,你是+
                    温度,Toast.LENGTH_SHORT);
            t.show();
        }
});
}
}
 

另外,activity_main是S-如下:

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:背景=@色/绿
>

<的TextView
    机器人:ID =@ + ID / textView1
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignParentTop =真
    机器人:layout_centerHorizo​​ntal =真
    机器人:layout_marginTop =76dp
    机器人:文本=@字符串/日期
    机器人:textAppearance =机器人:ATTR / textAppearanceLarge
    工具:忽略=硬codedText
    机器人:文字颜色=@色/蓝色
    机器人:textScaleX =0.9
     />

<的DatePicker
    机器人:ID =@ + ID / datePicker1
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_below =@ + ID / textView1
    机器人:layout_centerHorizo​​ntal =真
    机器人:layout_marginTop =61dp
    机器人:背景=@色/浅蓝色

    />

<按钮
    机器人:ID =@ + ID /按钮1
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_below =@ + ID / datePicker1
    机器人:layout_centerHorizo​​ntal =真
    机器人:layout_marginTop =54dp
    机器人:文本=
    工具:忽略=硬codedText
    机器人:背景=@可绘制/一
    />

< / RelativeLayout的>
 

解决方案

试试这个:

在MainActivity:

 意向意图=新的意图();
intent.setClass(这一点,Other_Activity.class);
intent.putExtra(EXTRA_ID,有些DATAS);
startActivity(意向);
 

在Other_Activity:

  @覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
捆绑额外= getIntent()getExtras()。
如果(临时演员!= NULL){
串DATAS = extras.getString(EXTRA_ID);
如果(DATAS!= NULL){
    //做的东西
}
}
 

另外检查: Android的意图

希望这有助于。

The result of first activity is displayed in the same screen(window)(activity).I just need to display the result in another activity.Can someone please tell me how to do that.The main activity is as follows :

package com.example.knowyourself;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Toast;
import android.view.View.OnClickListener;

public class MainActivity extends Activity 
{
Button submit;  
String temp;    
DatePicker dt;
int day, month, year, choice;

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

public void addListenerOnButton()
{
    submit = (Button) findViewById(R.id.button1);       
    dt = (DatePicker) findViewById(R.id.datePicker1);   
    submit.setOnClickListener(new OnClickListener()     
    {
        public void onClick(View v) 
        {
            day = dt.getDayOfMonth();   
            month = dt.getMonth();      
            year = dt.getYear();        
            choice = (day + month + year) % 5;
            switch (choice) 
            {
            ..............
                            ..............
            }
            Toast t = Toast.makeText(MainActivity.this, "You are" +           
                    temp,Toast.LENGTH_SHORT);                                   
            t.show();                                                       
        }
});
}
}

Also the activity_main is a s follows :

<RelativeLayout 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:background="@color/green"
>

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="76dp"
    android:text="@string/Date"
    android:textAppearance="?android:attr/textAppearanceLarge"
    tools:ignore="HardcodedText" 
    android:textColor="@color/blue"
    android:textScaleX="0.9"
     />

<DatePicker
    android:id="@+id/datePicker1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="61dp"
    android:background="@color/lightblue"

    />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/datePicker1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="54dp"
    android:text="     "
    tools:ignore="HardcodedText"
    android:background="@drawable/one"
    />

</RelativeLayout>

解决方案

Try this :

In MainActivity :

Intent intent = new Intent();
intent.setClass(this, Other_Activity.class);
intent.putExtra("EXTRA_ID", "SOME DATAS");
startActivity(intent);

In Other_Activity :

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String datas= extras.getString("EXTRA_ID");
if (datas!= null) {
    // do stuff
}        
}

Also check this : Android Intents

Hope this helps.

这篇关于需要结果显示在新活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆