접기
package com . example . a15u560 . autologin ;
import android . app . Activity ;
import android . content . Intent ;
import android . os . Bundle ;
import android . view . View ;
import android . widget . Button ;
import android . widget . EditText ;
public class MainActivity extends Activity {
EditText input ;
Button btn ;
int number ;
@Override
protected void onCreate ( Bundle savedInstanceState ) {
super . onCreate ( savedInstanceState ) ;
setContentView ( R . layout . activity_main ) ;
input = ( EditText ) findViewById ( R . id . inputNumber ) ;
btn = ( Button ) findViewById ( R . id . btn ) ;
btn . setOnClickListener ( new View. OnClickListener ( ) {
@Override
public void onClick ( View view ) {
number = Integer . parseInt ( input . getText ( ) . toString ( ) ) ;
Intent intent = new Intent ( MainActivity . this , SubActivity . class ) ;
intent . putExtra ( "input" , number ) ;
startActivity ( intent ) ;
finish ( ) ;
}
} ) ;
}
}
접기 접기
< ? 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"
android : gravity = "center" >
< LinearLayout
android : layout_width = "match_parent"
android : layout_height = "wrap_content"
android : orientation = "horizontal"
android : gravity = "center" >
< LinearLayout
android : layout_width = "wrap_content"
android : layout_height = "wrap_content"
android : orientation = "vertical" >
< EditText
android : layout_width = "200dp"
android : layout_height = "wrap_content"
android : textColor = "#000"
android : textSize = "20dp"
android : id = "@+id/inputNumber"
android : hint = "원하느 숫자를 입력"
android : textColorHint = "#c9c9c9"
android : inputType = "number" / >
< / LinearLayout >
< Button
android : layout_width = "wrap_content"
android : layout_height = "match_parent"
android : text = "결과보기"
android : textSize = "20dp"
android : id = "@+id/btn" / >
< / LinearLayout >
< / LinearLayout >
접기 접기
package com . example . a15u560 . autologin ;
import android . app . Activity ;
import android . content . Intent ;
import android . graphics . Color ;
import android . os . Bundle ;
import android . view . View ;
import android . view . ViewGroup ;
import android . widget . LinearLayout ;
import android . widget . TextView ;
import java . util . Random ;
public class SubActivity extends Activity {
LinearLayout dynamicLayout ;
@Override
protected void onCreate ( Bundle savedInstanceState ) {
super . onCreate ( savedInstanceState ) ;
setContentView ( R . layout . activity_sub ) ;
Intent intent = getIntent ( ) ;
int getNumber = intent . getExtras ( ) . getInt ( "input" ) ;
dynamicLayout = ( LinearLayout ) findViewById ( R . id . dynamicLayout ) ;
LinearLayout . LayoutParams param = new LinearLayout. LayoutParams ( ViewGroup . LayoutParams . MATCH_PARENT ,
ViewGroup . LayoutParams . MATCH_PARENT , 2 . 0f ) ;
for ( int i = 0 ; i < getNumber ; i ++ ) {
int R = new Random ( ) . nextInt ( 255 ) ;
int G = new Random ( ) . nextInt ( 255 ) ;
int B = new Random ( ) . nextInt ( 255 ) ;
TextView newTextView = new TextView ( this ) ;
newTextView . setText ( "동적 텍스트" + ( i + 1 ) ) ;
LinearLayout dynamicHori = new LinearLayout ( this ) ;
dynamicHori . setBackgroundColor ( Color . rgb ( R , G , B ) ) ;
dynamicHori . addView ( newTextView ) ;
dynamicLayout . addView ( dynamicHori ) ;
}
}
public void back ( View view ) {
Intent intent = new Intent ( SubActivity . this , MainActivity . class ) ;
startActivity ( intent ) ;
finish ( ) ;
}
}
접기 접기
< ? xml version = "1.0" encoding = "utf-8" ? >
< LinearLayout xmlns : android = "http://schemas.android.com/apk/res/android"
android : orientation = "vertical"
android : layout_width = "match_parent"
android : layout_height = "match_parent"
android : gravity = "center" >
< LinearLayout
android : layout_width = "wrap_content"
android : layout_height = "wrap_content"
android : orientation = "vertical"
android : gravity = "center"
android : id = "@+id/dynamicLayout" >
< / LinearLayout >
< Button
android : layout_width = "100dp"
android : layout_height = "100dp"
android : text = "뒤로가기"
android : onClick = "back" / >
< / LinearLayout >
접기