java>MainActivity.java
package com.example.ex03;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Main2Activity extends AppCompatActivity {
TextView txtResult;
EditText num1;
EditText num2;
int intnum1,intnum2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
num1=findViewById(R.id.num1);
num2=findViewById(R.id.num2);
txtResult=findViewById(R.id.txtResult);
//등록
registerForContextMenu(num1);
registerForContextMenu(txtResult);
}
public void mClick(View v){
int intnum1=Integer.parseInt(num1.getText().toString());
int intnum2=Integer.parseInt(num2.getText().toString());
switch (v.getId()){
case R.id.btn1:
int intResult=intnum1+intnum2;
txtResult.setText("더한결과:" +intResult);
break;
case R.id.btn2:
intResult=intnum1-intnum2;
txtResult.setText("뺀결과:" +intResult);
break;
case R.id.btn3:
intResult=intnum1*intnum2;
txtResult.setText("곱한결과:" +intResult);
break;
case R.id.btn4:
double doubleResult=(double)intnum1/intnum2;
txtResult.setText("나눈결과:" +doubleResult);
break;
}
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
if(v == num1){
menu.setHeaderTitle("에디트 메뉴");
menu.add(0,1,0,"빨강");
menu.add(0,2,0,"파랑");
}
if(v == txtResult){
menu.setHeaderTitle("텍스트 메뉴");
menu.add(0,3,0,"빨강");
menu.add(0,4,0,"파랑");
}
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case 1:
num2.setBackgroundColor(Color.RED);
break;
case 2:
num2.setBackgroundColor(Color.BLUE);
break;
case 3:
txtResult.setBackgroundColor(Color.RED);
break;
case 4:
txtResult.setBackgroundColor(Color.BLUE);
break;
}
return super.onContextItemSelected(item);
}
}
res>layout>activity_main2.xml
'Android' 카테고리의 다른 글
캘린더를 활용한 다이어리 앱 만들기 (0) | 2019.11.11 |
---|---|
주소록만들기(버튼,옵션메뉴) (0) | 2019.11.07 |
메뉴와 서브메뉴만들기 (0) | 2019.11.07 |
ListView,삭제버튼,입력버튼 (0) | 2019.11.06 |
사칙연산 화면 만들기 (0) | 2019.11.05 |