안드로이드 개발은 처음이라 조금 굴곡이 있었지만 다행히 이틀간의 노력 끝에 해낼 수 있었다.
우선res/layout의 폴더에activity 설정하기main.xml 파일
이것은 안드로이드 페이지 파일로 인터페이스에 대한 컨트롤은 모두 여기에 정의되어 있다. 예를 들어 안에 내용을 쓴다.
<TableRow>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:textSize="42sp" android:layout_weight="1">
<Button android:id="@+id/opLEFT" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:textSize="30sp"
android:text="(" android:layout_weight="1" />
<Button android:id="@+id/opRIGHT" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:textSize="30sp"
android:text=")" android:layout_weight="1" />
<Button android:id="@+id/opDEL" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:textSize="30sp"
android:text="DEL" android:layout_weight="1" />
<Button android:id="@+id/opCLS" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:textSize="30sp"
android:text="CLS" android:layout_weight="1" />
</LinearLayout>
</TableRow>
위의
는 한 줄로 웹 탭 와 유사합니다
탭은 이 줄에 선형 레이아웃을 추가합니다. 이 레이아웃 속성에 대한 정의가 있습니다. 탭은 단추를 추가하는 것입니다. id=@+id/opLEFT는 이 단추의 이름을 정의합니다. 백엔드에서 쉽게 호출할 수 있습니다. text="("는 단추가 표시되는 내용입니다. 즉, 이 단추는 왼쪽 괄호 키입니다. 아래의 각 버튼은 유사하니, 여기서는 더 이상 군말하지 않겠다. 이로써 실행 파일은 인터페이스에 하나의 단추를 표시할 수 있습니다. 비록 아무런 기능도 없지만, 다음은 어떻게 백그라운드에 기능을 추가하는지 소개할 것입니다. 왼쪽 괄호 단추를 예로 들다 MainActivity에서java의 onCreate () 함수에서 Button btn=new Button()과 같은 Button을 정의합니다. 그리고 인터페이스에 대응하는 단추를 지정합니다. btn=(Button)findViewById(R.id.opLEFT) opLEFT는 XML 파일에 정의된 이름이므로 사용할 수 있습니다. 이로써 우리는 btn을 조작하여 왼쪽 괄호가 실행하는 기능을 제어할 수 있게 되었다. 기능을 실현하려면 이벤트 감청기를 추가해야 한다 우리는 스스로 감청 함수를 실현한다. private class clickNumListener implements Button.OnClickListener{
@Override
public void onClick(View view)
{
Button btn = (Button)view;
String input = btn.getText().toString();
resultText.append(input);
}
}
그리고 감청기: btn.setOnClickListener(new clickNumListener()); 실현된 기능은resultText의 내용에 '(',resultText는 텍스트 상자로 그 정의 방식은btn과 유사합니다. 이로써 터치 기능이 이미 실현되었음을 기본적으로 보여 줍니다. 나머지 내용은 어떻게 실현되었는지 상세하게 설명하지 않고 코드 전문가에게 알립니다 MainActivity.java package example.mypro;
import java.util.Stack;
import android.R.string;
import android.support.v7.app.ActionBarActivity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
private Button[] btnNum = new Button[15];
private Button[] btnOp = new Button[11];
private EditText resultText=null ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnNum[ 0] = (Button) findViewById(R.id.num0);
btnNum[ 1] = (Button) findViewById(R.id.num1);
btnNum[ 2] = (Button) findViewById(R.id.num2);
btnNum[ 3] = (Button) findViewById(R.id.num3);
btnNum[ 4] = (Button) findViewById(R.id.num4);
btnNum[ 5] = (Button) findViewById(R.id.num5);
btnNum[ 6] = (Button) findViewById(R.id.num6);
btnNum[ 7] = (Button) findViewById(R.id.num7);
btnNum[ 8] = (Button) findViewById(R.id.num8);
btnNum[ 9] = (Button) findViewById(R.id.num9);
btnNum[ 10] = (Button) findViewById(R.id.opLEFT);
btnNum[ 11] = (Button) findViewById(R.id.opRIGHT);
btnNum[ 12] = (Button) findViewById(R.id.opPOI);
btnOp[ 1]=(Button) findViewById(R.id.opADD);
btnOp[ 2]=(Button) findViewById(R.id.opSUB);
btnOp[ 3]=(Button) findViewById(R.id.opDIV);
btnOp[ 4]=(Button) findViewById(R.id.opMUL);
btnOp[ 5]=(Button) findViewById(R.id.opEQU);
btnOp[ 6]=(Button) findViewById(R.id.opDEL);
btnOp[ 7]=(Button) findViewById(R.id.opCLS);
resultText =(EditText) findViewById(R.id.result);
for (int i=0;i<=12;i++)
btnNum[i].setOnClickListener( new clickNumListener());
for (int i=1;i<=4;i++)
btnOp[i].setOnClickListener( new clickNumListener());
btnOp[ 5].setOnClickListener(new clickOpListener());
btnOp[ 6].setOnClickListener(new clickOpListener());
btnOp[ 7].setOnClickListener(new clickOpListener());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true ;
}
private class clickNumListener implements Button.OnClickListener{
@Override
public void onClick(View view)
{
Button btn = (Button)view;
String input = btn.getText().toString();
resultText.append(input);
}
}
private class clickOpListener implements Button.OnClickListener{
@Override
public void onClick(View view)
{
Button btn = (Button)view;
String input = btn.getText().toString();
if (input.equals("="))
{
calculate();
}
else if (input.equals("DEL"))
{
if (resultText.getText().length()>0)
resultText.setText(resultText.getText().subSequence( 0, resultText.getText().length()-1));
}
else if (input.equals("CLS"))
{
resultText.setText( "");
}
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId())
{
case R.id.mi1:
break ;
case R.id.mi2:
break ;
case R.id.mi3:
break ;
case R.id.mi4:
break ;
}
return true ;
}
public void calculate()
{
char [] str=new char [100];
resultText.append( "#");
Stack <String> s=new Stack<String>();
Stack <String> s2=new Stack<String>();
s.push( "#");
int len1=0;
int count=999;
boolean flag=true ;
for (int i=0;i<resultText.getText().length();i++)
{
try
{
if ((resultText.getText().charAt(i)<='9'&&resultText.getText().charAt(i)>='0')||resultText.getText().charAt(i)=='.')
{
count =0;
str[len1 ++]=resultText.getText().charAt(i);
}
else if (resultText.getText().charAt(i)!=')'&&resultText.getText().charAt(i)!='#')
{
count ++;
if (count==1)
{
s2.push(String.valueOf(str));
len1 =0;
str =new char [100];
}
if (s.size()==1||resultText.getText().charAt(i)=='('||s.peek().charAt(0)=='(')
s.push(String.valueOf(resultText.getText().charAt(i)));
else if (resultText.getText().charAt(i)=='x'||resultText.getText().charAt(i)=='/')
{
if (s.peek().charAt(0)=='x'||s.peek().charAt(0)=='/')
{
char c=s.pop().charAt(0);
String num1 =s2.pop();
String num2 =s2.pop();
double num3;
if (c=='x')
{
num3 =Double.parseDouble(num1)*Double.parseDouble(num2);
s2.push(String.valueOf(num3));
}
else
{
num3 =Double.parseDouble(num2)/Double.parseDouble(num1);
s2.push(String.valueOf(num3));
}
s.push(String.valueOf(resultText.getText().charAt(i)));
}
else
{
s.push(String.valueOf(resultText.getText().charAt(i)));
}
}
else if (resultText.getText().charAt(i)=='+'||resultText.getText().charAt(i)=='-')
{
char c=s.pop().charAt(0);
String num1 =s2.pop();
String num2 =s2.pop();
double num3;
if (c=='x')
{
num3 =Double.parseDouble(num1)*Double.parseDouble(num2);
s2.push(String.valueOf(num3));
}
else if (c=='+')
{
num3 =Double.parseDouble(num2)+Double.parseDouble(num1);
s2.push(String.valueOf(num3));
}
else if (c=='-')
{
num3 =Double.parseDouble(num2)-Double.parseDouble(num1);
s2.push(String.valueOf(num3));
}
else
{
num3 =Double.parseDouble(num2)/Double.parseDouble(num1);
s2.push(String.valueOf(num3));
}
s.push(String.valueOf(resultText.getText().charAt(i)));
}
}
else
{
count ++;
if (count==1)
{
s2.push(String.valueOf(str));
len1 =0;
str =new char [100];
}
if (resultText.getText().charAt(i)==')')
{
while (true )
{
char c=s.pop().charAt(0);
if (c=='(')
break ;
String num1 =s2.pop();
String num2 =s2.pop();
double num3;
if (c=='+')
{
num3 =Double.parseDouble(num2)+Double.parseDouble(num1);
s2.push(String.valueOf(num3));
}
else if (c=='-')
{
num3 =Double.parseDouble(num2)-Double.parseDouble(num1);
s2.push(String.valueOf(num3));
}
else if (c=='x')
{
num3 =Double.parseDouble(num2)*Double.parseDouble(num1);
s2.push(String.valueOf(num3));
}
else if (c=='/')
{
num3 =Double.parseDouble(num2)/Double.parseDouble(num1);
s2.push(String.valueOf(num3));
}
}
}
else
{
while (true )
{
char c=s.pop().charAt(0);
if (c=='#')
break ;
String num1 =s2.pop();
String num2 =s2.pop();
double num3;
if (c=='+')
{
num3 =Double.parseDouble(num2)+Double.parseDouble(num1);
s2.push(String.valueOf(num3));
}
else if (c=='-')
{
num3 =Double.parseDouble(num2)-Double.parseDouble(num1);
s2.push(String.valueOf(num3));
}
else if (c=='x')
{
num3 =Double.parseDouble(num2)*Double.parseDouble(num1);
s2.push(String.valueOf(num3));
}
else if (c=='/')
{
num3 =Double.parseDouble(num2)/Double.parseDouble(num1);
s2.push(String.valueOf(num3));
}
}
}
}
}
catch (Exception e)
{
flag =false ;
resultText.setText( "Error");
break ;
}
}
if (flag)
resultText.setText(s2.pop());
}
}
activity_main.xml <?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width ="fill_parent" android:layout_height="wrap_content"
android:textSize ="42sp" android:stretchColumns="1">
<TableRow>
<EditText android:id="@+id/result" android:layout_width="fill_parent"
android:layout_height ="wrap_content"
android:background ="@android:drawable/editbox_background"
android:layout_span ="4" android:textSize="30sp" android:gravity="right|center_vertical"
android:cursorVisible ="false" android:editable="false" android:lines="1" />
</TableRow>
<TableRow>
<LinearLayout android:orientation="horizontal"
android:layout_width ="fill_parent" android:layout_height="wrap_content"
android:textSize ="42sp" android:layout_weight="1">
<Button android:id="@+id/opLEFT" android:layout_width="fill_parent"
android:layout_height ="wrap_content" android:textSize="30sp"
android:text ="(" android:layout_weight="1" />
<Button android:id="@+id/opRIGHT" android:layout_width="fill_parent"
android:layout_height ="wrap_content" android:textSize="30sp"
android:text =")" android:layout_weight="1" />
<Button android:id="@+id/opDEL" android:layout_width="fill_parent"
android:layout_height ="wrap_content" android:textSize="30sp"
android:text ="DEL" android:layout_weight="1" />
<Button android:id="@+id/opCLS" android:layout_width="fill_parent"
android:layout_height ="wrap_content" android:textSize="30sp"
android:text ="CLS" android:layout_weight="1" />
</LinearLayout>
</TableRow>
<TableRow>
<LinearLayout android:orientation="horizontal"
android:layout_width ="fill_parent" android:layout_height="wrap_content"
android:textSize ="42sp" android:layout_weight="1">
<Button android:id="@+id/num7" android:layout_width="fill_parent"
android:layout_height ="wrap_content" android:textSize="30sp"
android:text ="7" android:layout_weight="1" />
<Button android:id="@+id/num8" android:layout_width="fill_parent"
android:layout_height ="wrap_content" android:textSize="30sp"
android:text ="8" android:layout_weight="1" />
<Button android:id="@+id/num9" android:layout_width="fill_parent"
android:layout_height ="wrap_content" android:textSize="30sp"
android:text ="9" android:layout_weight="1" />
<Button android:id="@+id/opDIV" android:layout_width="fill_parent"
android:layout_height ="wrap_content" android:textSize="30sp"
android:text ="/" android:layout_weight="1" />
</LinearLayout>
</TableRow>
<TableRow>
<LinearLayout android:orientation="horizontal"
android:layout_width ="fill_parent" android:layout_height="wrap_content"
android:textSize ="42sp" android:layout_weight="1">
<Button android:id="@+id/num4" android:layout_width="fill_parent"
android:layout_height ="wrap_content" android:textSize="30sp"
android:text ="4" android:layout_weight="1" />
<Button android:id="@+id/num5" android:layout_width="fill_parent"
android:layout_height ="wrap_content" android:textSize="30sp"
android:text ="5" android:layout_weight="1" />
<Button android:id="@+id/num6" android:layout_width="fill_parent"
android:layout_height ="wrap_content" android:textSize="30sp"
android:text ="6" android:layout_weight="1" />
<Button android:id="@+id/opMUL" android:layout_width="fill_parent"
android:layout_height ="wrap_content" android:textSize="30sp"
android:text ="x" android:layout_weight="1" />
</LinearLayout>
</TableRow>
<TableRow>
<LinearLayout android:orientation="horizontal"
android:layout_width ="fill_parent" android:layout_height="wrap_content"
android:textSize ="42sp" android:layout_weight="1">
<Button android:id="@+id/num1" android:layout_width="fill_parent"
android:layout_height ="wrap_content" android:textSize="30sp"
android:text ="1" android:layout_weight="1" />
<Button android:id="@+id/num2" android:layout_width="fill_parent"
android:layout_height ="wrap_content" android:textSize="30sp"
android:text ="2" android:layout_weight="1" />
<Button android:id="@+id/num3" android:layout_width="fill_parent"
android:layout_height ="wrap_content" android:textSize="30sp"
android:text ="3" android:layout_weight="1" />
<Button android:id="@+id/opSUB" android:layout_width="fill_parent"
android:layout_height ="wrap_content" android:textSize="30sp"
android:text ="-" android:layout_weight="1" />
</LinearLayout>
</TableRow>
<TableRow>
<LinearLayout android:orientation="horizontal"
android:layout_width ="fill_parent" android:layout_height="wrap_content"
android:textSize ="30sp" android:layout_weight="1">
<Button android:id="@+id/opPOI" android:layout_width="fill_parent"
android:layout_height ="wrap_content" android:textSize="30sp"
android:text ="." android:layout_weight="1" />
<Button android:id="@+id/num0" android:layout_width="fill_parent"
android:layout_height ="wrap_content" android:textSize="30sp"
android:text ="0" android:layout_weight="1" />
<Button android:id="@+id/opEQU" android:layout_width="fill_parent"
android:layout_height ="wrap_content" android:textSize="30sp"
android:text ="=" android:layout_weight="1" />
<Button android:id="@+id/opADD" android:layout_width="fill_parent"
android:layout_height ="wrap_content" android:textSize="30sp"
android:text ="+" android:layout_weight="1" />
</LinearLayout>
</TableRow>
<TableRow>
<TextView android:id="@+id/show" android:layout_width="fill_parent"
android:text =" , , " android:layout_height="wrap_content" android:gravity="center"/>
</TableRow>
</TableLayout>
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
[Android] 둥글게 펼쳐지는 Ripple을, 바삭하게 구현
간이적으로 터치 피드백이 없는 버튼이나 레이아웃, 탭 범위가 좁아져 버린 버튼 등에, 범위 밖으로 둥글게 퍼지는 Ripple로 탭감, 영역을 조금 늘립니다.
이런 느낌 (화질 나쁘고 미안해..)
Ripple을 내고 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.