2013년 9월 27일 금요일

[AnDrOiD] WebView 의 간단한 예제

public class MainActivity extends Activity {

     @Override
     protected void onCreate(Bundle savedInstanceState) {
     requestWindowFeature(Window.FEATURE_NO_TITLE); // 타이틀바 없음
     super.onCreate(savedInstanceState);

     setContentView(R.layout.activity_main);

     WebView webView = (WebView)findViewById(R.id.webView);

     webView.setWebViewClient(new WebViewClient());

     webView.clearCache(true); // 캐쉬 삭제

     webView.getSettings().setJavaScriptEnabled(true); // 자바스크립트 사용 여부

     /*
     webView.getSettings().setSaveFormData(false); // 비밀번호저장 사용 안함
     webView.getSettings().setSavePassword(false); // 폼 데이터 저장(자동완성) 사용 안함
     webView.getSettings().setBuiltInZoomControls(false); // 멀티줌 사용 안함
     webView.getSettings().setSupportZoom(false); // 줌 사용 안함

     webView.setVerticalScrollBarEnabled(false); // 세로 스크롤바 사용 안함
     webView.setHorizontalScrollBarEnabled(false); // 가로 스크롤바 사용 안함
     */

     webView.loadUrl("http://google.com");
}

     @Override
     public boolean onKeyDown(int keyCode, KeyEvent event) {
     // TODO Auto-generated method stub

     if(keyCode == KeyEvent.KEYCODE_BACK){
     if(webView.canGoBack()){
     webView.goBack();
     return false;
     } else{
     // else 인 경우 코드 추가
     }

     return super.onKeyDown(keyCode, event);
     }
}

[AnDrOiD] 타이틀바 안보이도록 설정

1. Using JAVA CODE
requestWindowFeature(Window.FEATURE_NO_TITLE);

2. Using AndroidManifast.xml
<activity android:name=".ActivityMain" android:theme="@android:style/Theme.NoTitleBar">

<activity android:name=".ActivityMain" android:theme="@android:style/Theme.NoTitleBar.Fullscreen">

2013년 9월 25일 수요일

[AnDrOiD] inflater 의 간단한 예제

   @Override
   protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
 
   LinearLayout linearLayout = (LinearLayout)findViewById(R.id.linearLayout);
   LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   inflater.inflate(R.layout.activity_test, linearLayout, true);

   }

[AnDrOiD] xml 파일 include 예제

1. activity_main.xml 의 내용
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <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:layout_weight="1"
            android:gravity="center_horizontal"
            android:text="테스트" />
      
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:inputType="numberSigned" />

    </LinearLayout>
  
   <include layout="@layout/activity_import_test" />

</LinearLayout>


2. activity_import_test.xml 의 내용
<?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:orientation="vertical" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="테스트용 입니다." />

</LinearLayout>

2013년 9월 23일 월요일

[AnDrOiD] 파라메터 전달 간단한 예제


MainActivity.java 의 파일 내용
package com.example.androidexam;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements View.OnClickListener{

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  Button btnTest1 = (Button)findViewById(R.id.btnTest1);
  btnTest1.setOnClickListener(this);
  
}

@Override
public void onClick(View v) {
  // TODO Auto-generated method stub
  
  switch(v.getId()){
  case R.id.btnTest1:
   Intent intent = new Intent(getBaseContext(), SubActivity.class);
   intent.putExtra("name", "홍길동");
   intent.putExtra("age", 29);
   startActivity(intent);
   
   break;
   
  case R.id.btnTest2:
   Toast.makeText(getBaseContext(), "btnTest2 클릭", Toast.LENGTH_SHORT).show();
   break;
   
  default:
    
   break;
  }
}
}



SubActivity.java 의 파일 내용
package com.example.androidexam;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;


public class SubActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

  // TODO Auto-generated method stub

  super.onCreate(savedInstanceState);

  setContentView(R.layout.activity_sub);

  Intent intent = new Intent(this.getIntent());

  String name = intent.getStringExtra("name");

  int age = intent.getIntExtra("age", 1);

  Toast.makeText(getBaseContext(), name + "님의 나이는 " + age + "입니다.", Toast.LENGTH_SHORT).show();

  }

}