// 아이디: xxx 비밀번호:xxx
(1) activity_main.xml
; activity main의 layout 변경 - Graphical layout에서 화면에 마우스 우측 - change layout - 원하는 layout 선택
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="아이디 입력"
/>
<EditText android:id="@+id/id"
android:layout_width="200dp"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="비밀번호입력"
/>
<EditText android:id="@+id/pwd"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:password="true"
/>
</LinearLayout>
<Button android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="로그인"/>
</LinearLayout>
(2) MainActivity.java
; 인터페이스를 구현하지 않고 익명의 내부클래스 사용.
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
// Quiz
//로그인 버튼을 클릭하면 입력된 아이디와 비밀번호를 얻어와 토스트로 출력해 보세요.
// 아이디: xxx 비밀번호:xxx
public class MainActivity extends Activity {
EditText id;
EditText pwd;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
id = (EditText)findViewById(R.id.id);
pwd= (EditText)findViewById(R.id.pwd);
btn= (Button)findViewById(R.id.btn);
@Override
public void onClick(View v) {
String strId = id.getText().toString();
String strPwd= pwd.getText().toString();
//익명의 내부클래스 사용시 this는 new생성 객체가 되기 때문에 오류가 난다.
//이때는 MainActivity자신을 의미하기 때문에 MainActivity.this로 넘긴다.
Toast.makeText(MainActivity.this, "아이디:" + strId + " 비밀번호:" + strPwd, Toast.LENGTH_LONG ).show();
}
} );
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
'Mobile > Android' 카테고리의 다른 글
#06 (이미지 뷰) (0) | 2013.02.02 |
---|---|
#05 ( checkbox, radiobutton ) (0) | 2013.01.29 |
#03( 합구하기 ) (0) | 2013.01.28 |
#02 ( 시작하기 ) (0) | 2013.01.28 |
#01( Android Virtual Device Manager ) (0) | 2013.01.28 |