activity_main.xml

<Button
android:id="@+id/btn01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="AlertDialog" />

<Button
android:id="@+id/btn02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="CustomDialog" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="hahahohohehe"
android:textSize="20dp"/>
<TextView
android:id="@+id/tv01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textStyle="bold"/>
</LinearLayout>

custom.xml

<ImageButton
android:id="@+id/imgBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/sym_def_app_icon"/>
<EditText
android:id="@+id/editText01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint = "put expected one!!!"/>

MainActivity.java

public class MainActivity extends AppCompatActivity {
Button btn_alert;
Button btn_custom;
TextView textView;

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

btn_alert = findViewById(R.id.btn01);
btn_custom = findViewById(R.id.btn02);
textView = findViewById(R.id.tv01);

btn_alert.setOnClickListener(listener);
btn_custom.setOnClickListener(listener);
}
View.OnClickListener listener = new View.OnClickListener(){
public void onClick(View v){
switch (v.getId()){
case R.id.btn01:
new AlertDialog.Builder(MainActivity.this)
.setTitle("aabbccddeefff")
.setMessage("12312312313")
.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton("YES", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int btn){
Toast.makeText(getBaseContext(), "YES", Toast.LENGTH_LONG).show();
textView.setText("ILikeEliphant");
}
})
.setNegativeButton("NO", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int btn){
Toast.makeText(getBaseContext(), "NO", Toast.LENGTH_LONG).show();
textView.setText("choose again");
}
})
.show();
break;
case R.id.btn02:
LinearLayout linearLayout = (LinearLayout)View.inflate(MainActivity.this, R.layout.custom, null);
final EditText editText = linearLayout.findViewById(R.id.editText01);

new AlertDialog.Builder(MainActivity.this)
.setTitle("hehehehehehehe")
.setView(linearLayout)
.setPositiveButton("OK", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int btn){
Toast.makeText(getBaseContext(), "OK", Toast.LENGTH_LONG).show();
String winner = editText.getText().toString();
textView.setText(winner);
}
})
.setNegativeButton("CANCEL", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int btn){
Toast.makeText(MainActivity.this, "CANCEL", Toast.LENGTH_LONG).show();
}
})
.show();
break;
}
}
};
}

'android studio' 카테고리의 다른 글

activity 추가하기  (0) 2019.08.13
Recycler View  (0) 2019.08.13
alert dialog  (0) 2019.08.13
방향전환 event로 activity 유지하기  (0) 2019.08.13
방향전환시 상태값 저장  (0) 2019.08.12

+ Recent posts