내가 입력한 글자 Toast로 나오게 하기

by 조쉬 posted Jul 26, 2015
?

단축키

Prev이전 문서

Next다음 문서

ESC닫기

크게 작게 위로 아래로 댓글로 가기 인쇄

MainActivity 소스

 

public class MainActivity extends Activity {

 

@Override

protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.activity_main);

 

  if (savedInstanceState == null) {

   getFragmentManager().beginTransaction()

     .add(R.id.container, new PlaceholderFragment()).commit();

  }

}

 

public void sendMessage(View view) {

  Intent intent = getIntent();

  EditText editText = (EditText)findViewById(R.id.input);

  String message = editText.getText().toString();

  Toast.makeText(this,message,Toast.LENGTH_SHORT).show();

}




=====================================================================================



프레그먼트_메인.xml 소스

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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"
    tools:context="com.example.test.MainActivity$PlaceholderFragment" >

    <EditText 
        android:id="@+id/input"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="@string/nameInput"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/input"
        android:layout_marginLeft="21dp"
        android:layout_toRightOf="@+id/input"
        android:text="@string/send" 
        android:onClick="sendMessage"/>

</RelativeLayout>


=====================================================================================



strings.xml 소스

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Test</string>
    <string name="action_settings">Settings</string>
    <string name="nameInput">이름을 입력하세요</string>
    <string name="send">전송</string>

</resources>


=====================================================================================

실행화면