ITGenerations
안드로이드 - 버튼, 새로운 창에 새로운 버튼 클릭 본문
자바
package com.example.ryan.myapplication;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//login method
public void onButtonClicked(View view){
final Dialog loginDialog = new Dialog(this);
loginDialog.setContentView(R.layout.second);
loginDialog.setTitle("로그인화면");
Button login = (Button)loginDialog.findViewById(R.id.login);
Button cancel = (Button)loginDialog.findViewById(R.id.cancel);
final EditText username =(EditText)loginDialog.findViewById(R.id.username);
final EditText password = (EditText)loginDialog.findViewById(R.id.password);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(username.getText().toString().trim().length()>0&&password.getText().toString().trim().length()>0){
Toast.makeText(getApplicationContext(),"로그인성공",Toast.LENGTH_SHORT).show();
loginDialog.dismiss();
}
else {
Toast.makeText(getApplicationContext(),"다시입력하세요",Toast.LENGTH_SHORT).show();
}
}
});
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
loginDialog.dismiss();
}
});
loginDialog.show();
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.ryan.myapplication.MainActivity">
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onButtonClicked"
android:text="login" />
</LinearLayout>
xml2
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:orientation="vertical">
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="username"
android:inputType="textPersonName" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="password"
android:inputType="textPersonName" />
<Button
android:id="@+id/login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="login" />
<Button
android:id="@+id/cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@android:string/cancel" />
</LinearLayout>
'프로그래밍 > 안드로이드특강' 카테고리의 다른 글
안드로이드 - intent (0) | 2018.01.24 |
---|---|
안드로이드 - 터치 (0) | 2018.01.23 |
버튼상자생성 - 빨강, 녹색, 파랑 중 선택 (0) | 2018.01.23 |
버튼 yes, no (0) | 2018.01.23 |
계산기 소스 (0) | 2018.01.22 |