我想一个例子Android应用程序,它使用onResume,ONSTART和onRestart。
解决方案 com.test包;
这里进口的东西
公共类Pick_Color扩展活动实现OnClickListener
{
私人按钮B11;
私人按钮B12;
私人按钮B13;
私人按钮B14;
私人按钮b_final;
私人的EditText红色;
私人的EditText绿色;
私人的EditText蓝色;
静态INT temp_red;
静态INT temp_green;
静态INT temp_blue;
公共无效的onCreate(包savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.pickcolor);
Log.d(挑颜色,上创建);
B11 =(按钮)findViewById(R.id.buttonR1C1);
B12 =(按钮)findViewById(R.id.buttonR1C2);
B13 =(按钮)findViewById(R.id.buttonR1C3);
B14 =(按钮)findViewById(R.id.buttonR1C4);
b11.setBackgroundColor(Color.argb(255,255,255,0));
b12.setBackgroundColor(Color.argb(255,255,0255));
b13.setBackgroundColor(Color.argb(255,0,255255));
b14.setBackgroundColor(Color.argb(255,102,153,51));
b11.setOnClickListener(本);
b12.setOnClickListener(本);
b13.setOnClickListener(本);
b14.setOnClickListener(本);
红色=(EditText上)findViewById(R.id.RED_TEXT);
绿色=(EditText上)findViewById(R.id.GREEN_TEXT);
BLUE =(EditText上)findViewById(R.id.BLUE_TEXT);
RED.setText(Integer.toString(temp_red));
GREEN.setText(Integer.toString(temp_green));
BLUE.setText(Integer.toString(temp_blue));
b_final =(按钮)findViewById(R.id.button_result);
b_final.setBackgroundColor(Color.argb(255,temp_red,temp_green,temp_blue));
b_final.setOnClickListener(本);
OnKeyListener text_listener =新OnKeyListener()
{
公共布尔onKey(视图V,INT关键code,KeyEvent的事件)
{
INT标志= 0;
尝试
{
开关(v.getId())
{
案例R.id.RED_TEXT:
标志= 1;
temp_red =的Integer.parseInt(RED.getText()的toString());
如果(temp_red℃,|| temp_red> 255)
抛出新NumberFormatException的();
// Log.d(Pick_Color,temp_red =+ temp_red);
打破;
案例R.id.GREEN_TEXT:
标志= 2;
temp_green =的Integer.parseInt(GREEN.getText()的toString());
如果(temp_green℃,|| temp_green> 255)
抛出新NumberFormatException的();
// Log.d(Pick_Color,temp_green =+ temp_green);
打破;
案例R.id.BLUE_TEXT:
标志= 3;
temp_blue =的Integer.parseInt(BLUE.getText()的toString());
如果(temp_blue℃,|| temp_blue> 255)
抛出新NumberFormatException的();
// Log.d(Pick_Color,temp_blue =+ temp_blue);
打破;
}
}
赶上(NumberFormatException的N)
{
如果(标志== 1)
{
RED.setText();
Log.d(选取颜色,红色错误);
}
如果(标志== 2)
{
GREEN.setText();
Log.d(选取颜色,绿色错误);
}
如果(旗== 3)
{
BLUE.setText();
Log.d(选取颜色,蓝色错误);
}
}
Log.d(选取颜色,temp_red =+ temp_red +temp_green =+ temp_green +temp_blue =+ temp_blue +);
b_final.setBackgroundColor(Color.argb(255,temp_red,temp_green,temp_blue));
返回false;
}
};
RED.setOnKeyListener(text_listener);
GREEN.setOnKeyListener(text_listener);
BLUE.setOnKeyListener(text_listener);
}
保护无效onResume()
{
super.onResume();
RED.setText(Integer.toString(temp_red));
GREEN.setText(Integer.toString(temp_green));
BLUE.setText(Integer.toString(temp_blue));
b_final.setBackgroundColor(Color.argb(255,temp_red,temp_green,temp_blue));
}
公共无效的onClick(视图v)
{
开关(v.getId())
{
// - - - - - - - 第一排 - - - - - - -
案例R.id.buttonR1C1:
RED.setText(255);
GREEN.setText(255);
BLUE.setText(0);
temp_red =的Integer.parseInt(RED.getText()的toString());
temp_green =的Integer.parseInt(GREEN.getText()的toString());
temp_blue =的Integer.parseInt(BLUE.getText()的toString());
b_final.setBackgroundColor(Color.argb(255,255,255,0));
writeColor(颜色255 255 255 0);
打破;
案例R.id.buttonR1C2:
RED.setText(255);
GREEN.setText(0);
BLUE.setText(255);
temp_red =的Integer.parseInt(RED.getText()的toString());
temp_green =的Integer.parseInt(GREEN.getText()的toString());
temp_blue =的Integer.parseInt(BLUE.getText()的toString());
b_final.setBackgroundColor(Color.argb(255,255,0,255));
writeColor(颜色255 255 0 255);
打破;
案例R.id.buttonR1C3:
RED.setText(0);
GREEN.setText(255);
BLUE.setText(255);
temp_red =的Integer.parseInt(RED.getText()的toString());
temp_green =的Integer.parseInt(GREEN.getText()的toString());
temp_blue =的Integer.parseInt(BLUE.getText()的toString());
b_final.setBackgroundColor(Color.argb(255,0,255,255));
writeColor(颜色255 0 255 255);
打破;
案例R.id.buttonR1C4:
RED.setText(102);
GREEN.setText(153);
BLUE.setText(51);
temp_red =的Integer.parseInt(RED.getText()的toString());
temp_green =的Integer.parseInt(GREEN.getText()的toString());
temp_blue =的Integer.parseInt(BLUE.getText()的toString());
b_final.setBackgroundColor(Color.argb(255,102,153,51));
writeColor(色255 102 153 51);
打破;
案例R.id.button_result:
writeColor(色255+ temp_red ++ temp_green ++ temp_blue);
完();
打破;
}
}
公共无效writeColor(字符串字符串)
{
TouchList.Current_Color =串;
尝试
{
TouchList.dos.writeUTF(TouchList.Current_Color);
}
赶上(IOException异常E)
{
e.printStackTrace();
}
}
}
I would like an example Android app which uses onResume, onStart and onRestart.
解决方案package com.test;
import stuff here
public class Pick_Color extends Activity implements OnClickListener
{
private Button b11;
private Button b12;
private Button b13;
private Button b14;
private Button b_final;
private EditText RED;
private EditText GREEN;
private EditText BLUE;
static int temp_red;
static int temp_green;
static int temp_blue;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.pickcolor);
Log.d("pick color","on create");
b11=(Button)findViewById(R.id.buttonR1C1);
b12=(Button)findViewById(R.id.buttonR1C2);
b13=(Button)findViewById(R.id.buttonR1C3);
b14=(Button)findViewById(R.id.buttonR1C4);
b11.setBackgroundColor(Color.argb(255, 255, 255,0));
b12.setBackgroundColor(Color.argb(255, 255, 0,255));
b13.setBackgroundColor(Color.argb(255, 0, 255,255));
b14.setBackgroundColor(Color.argb(255, 102, 153,51));
b11.setOnClickListener(this);
b12.setOnClickListener(this);
b13.setOnClickListener(this);
b14.setOnClickListener(this);
RED=(EditText) findViewById(R.id.RED_TEXT);
GREEN=(EditText) findViewById(R.id.GREEN_TEXT);
BLUE=(EditText) findViewById(R.id.BLUE_TEXT);
RED.setText(Integer.toString(temp_red));
GREEN.setText(Integer.toString(temp_green));
BLUE.setText(Integer.toString(temp_blue));
b_final=(Button)findViewById(R.id.button_result);
b_final.setBackgroundColor(Color.argb(255,temp_red,temp_green,temp_blue));
b_final.setOnClickListener(this);
OnKeyListener text_listener= new OnKeyListener()
{
public boolean onKey(View v, int keyCode, KeyEvent event)
{
int flag=0;
try
{
switch(v.getId())
{
case R.id.RED_TEXT:
flag=1;
temp_red=Integer.parseInt(RED.getText().toString());
if(temp_red<0 || temp_red>255)
throw new NumberFormatException();
// Log.d("Pick_Color","temp_red = "+temp_red);
break;
case R.id.GREEN_TEXT:
flag=2;
temp_green=Integer.parseInt(GREEN.getText().toString());
if(temp_green<0 || temp_green>255)
throw new NumberFormatException();
// Log.d("Pick_Color","temp_green = "+temp_green);
break;
case R.id.BLUE_TEXT:
flag=3;
temp_blue=Integer.parseInt(BLUE.getText().toString());
if(temp_blue<0 || temp_blue>255)
throw new NumberFormatException();
// Log.d("Pick_Color","temp_blue = "+temp_blue);
break;
}
}
catch(NumberFormatException n)
{
if(flag==1)
{
RED.setText("");
Log.d("Pick Color","Red Error");
}
if(flag==2)
{
GREEN.setText("");
Log.d("Pick Color","Green Error");
}
if(flag==3)
{
BLUE.setText("");
Log.d("Pick Color","Blue Error");
}
}
Log.d("Pick Color","temp_red = "+temp_red+" temp_green = "+temp_green+" temp_blue = "+temp_blue+"");
b_final.setBackgroundColor(Color.argb(255,temp_red, temp_green, temp_blue));
return false;
}
};
RED.setOnKeyListener(text_listener);
GREEN.setOnKeyListener(text_listener);
BLUE.setOnKeyListener(text_listener);
}
protected void onResume()
{
super.onResume();
RED.setText(Integer.toString(temp_red));
GREEN.setText(Integer.toString(temp_green));
BLUE.setText(Integer.toString(temp_blue));
b_final.setBackgroundColor(Color.argb(255,temp_red,temp_green,temp_blue));
}
public void onClick(View v)
{
switch (v.getId())
{
// -------------- FIRST ROW --------------
case R.id.buttonR1C1:
RED.setText("255");
GREEN.setText("255");
BLUE.setText("0");
temp_red=Integer.parseInt(RED.getText().toString());
temp_green=Integer.parseInt(GREEN.getText().toString());
temp_blue=Integer.parseInt(BLUE.getText().toString());
b_final.setBackgroundColor(Color.argb(255,255,255,0));
writeColor("Color 255 255 255 0");
break;
case R.id.buttonR1C2:
RED.setText("255");
GREEN.setText("0");
BLUE.setText("255");
temp_red=Integer.parseInt(RED.getText().toString());
temp_green=Integer.parseInt(GREEN.getText().toString());
temp_blue=Integer.parseInt(BLUE.getText().toString());
b_final.setBackgroundColor(Color.argb(255,255,0,255));
writeColor("Color 255 255 0 255");
break;
case R.id.buttonR1C3:
RED.setText("0");
GREEN.setText("255");
BLUE.setText("255");
temp_red=Integer.parseInt(RED.getText().toString());
temp_green=Integer.parseInt(GREEN.getText().toString());
temp_blue=Integer.parseInt(BLUE.getText().toString());
b_final.setBackgroundColor(Color.argb(255,0,255,255));
writeColor("Color 255 0 255 255");
break;
case R.id.buttonR1C4:
RED.setText("102");
GREEN.setText("153");
BLUE.setText("51");
temp_red=Integer.parseInt(RED.getText().toString());
temp_green=Integer.parseInt(GREEN.getText().toString());
temp_blue=Integer.parseInt(BLUE.getText().toString());
b_final.setBackgroundColor(Color.argb(255,102,153,51));
writeColor("Color 255 102 153 51");
break;
case R.id.button_result:
writeColor("Color 255 "+temp_red+" "+temp_green+" "+temp_blue);
finish();
break;
}
}
public void writeColor(String string)
{
TouchList.Current_Color=string;
try
{
TouchList.dos.writeUTF(TouchList.Current_Color);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}