다른 Activity 호출

5329 단어 Activity

Google 공식 문서 참조 Traning / Getting Started / Building a simple user interface, Startinganother activity, http://developer.android.com/training/basics/firstapp/building-ui.html
1. 주 활동 만 들 기
이 클립 스 새 항목 MyFirst App 을 사용 하면 UI 레이아웃 은 다음 과 같 습 니 다.
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="horizontal"
    tools:context=".MainActivity">
 
    <EditText
        android:id="@+id/et_message"
        android:layout_height="wrap_content"
       android:layout_width="0dp"
        android:layout_weight="1"
        android:hint="@string/input_here"/>
   
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/click"
        android:onClick="sendMessage"/>
 
</LinearLayout>

권 리 를 통 해 사 이 즈 를 분배 하 는 방식 에 주의 하 세 요.
구성 요소 1:
android:layout_width="0dp"
android:layout_weight="1"

구성 요소 2:
android:layout_width="wrap_content"

2. 주 클래스 에서 onclick 에 대응 하 는 sendmessage 방법 을 지정 합 니 다.
package com.lujinhong.androidtraningmyfirstapp;
 
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
 
public class MainActivity extends Activity{
   
     public final static String EXTRA_MESSAGE="com.lujinhong.myfirstapp.MESSAGE";
 
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        // Inflate themenu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
   
    public void sendMessage(View v){
                   
        EditText et_message=(EditText)this.findViewById(R.id.et_message);
        String message= et_message.getText().toString().trim();
       
        Intent intent= new Intent(this,DisplayMessageActivity.class);
        intent.putExtra(EXTRA_MESSAGE, message);
       
        this.startActivity(intent);
       
    }
 
}

(1) intent 에 대하 여
An  Intent  isan object that provides runtime binding between separate components (such astwo activities). The Intent  representsan app’s "intent to do something." You can use intents for a widevariety of tasks, but most often they’re used to startanother activity.
(2) 다른 activity 를 호출 하 는 절차:
l  먼저 editText 의 텍스트 를 가 져 옵 니 다.
EditText et_message = (EditText) this.findViewById(R.id.et_message);
String message = et_message.getText().toString().trim();

l  그리고 인 텐트 를 만 들 고 K - V 형식 으로 인 텐트 에 텍스트 를 저장 합 니 다.
        Intent intent= new Intent(this,DisplayMessageActivity.class);
        intent.putExtra(EXTRA_MESSAGE, message);

intent 를 만 들 때 클래스 이름 을 통 해 어떤 종류의 파일 을 호출 할 지 지정 합 니 다.
l  마지막 으로 새로운 activity 를 시작 합 니 다.
this.startActivity(intent);

3. 다른 액 티 비 티 보이 기
package com.lujinhong.androidtraningmyfirstapp;
 
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TextView;
 
public class DisplayMessageActivityextends Activity{
 
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_message);
       
        // Get the messagefrom the intent
        Intent intent= getIntent();
        String message= intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
 
        // Create the textview
        TextView textView=new TextView(this);
        textView.setTextSize(40);
        textView.setText(message);
 
        // Set the textview as the activity layout
        setContentView(textView);
       
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        // Inflate themenu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.display_message, menu);
        return true;
    }
 
}

좋은 웹페이지 즐겨찾기