主页:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
public class MainActivity extends Activity
{ /**
* 主页点击按钮跳转到第二页
* 第二页点击按钮1将字符串内容显示到主页,点击按钮2将数字内容显示到主页
*/
private final static int RESULT_CODE_ONE = 1 ;
private final static int RESULT_CODE_TWO = 2 ;
@Override
protected void onActivityResult( int requestCode, int resultCode, Intent data)
{
super .onActivityResult(requestCode, resultCode, data);
TextView textView = (TextView) findViewById(R.id.textView1_main);
String text = null ;
switch (resultCode) //判断返回码
{
case RESULT_CODE_ONE:
text = "from按钮1:" + data.getStringExtra( "btnone" );
break ;
case RESULT_CODE_TWO:
int age = data.getIntExtra( "btntwo" , 0 ); //0为默认值,当data没有接收到返回的int时,返回这个默认值
text = "from按钮2:" + String.valueOf(age);
break ;
default :
break ;
}
textView.setText(text);
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button1_main).setOnClickListener( new OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent(MainActivity. this , SecondActivity. class );
startActivityForResult(intent , 0 );
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main, menu);
return true ;
}
} |
第二页
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
public class SecondActivity extends Activity
{ private final static int RESULT_CODE_ONE = 1 ;
private final static int RESULT_CODE_TWO = 2 ;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
final Intent data = new Intent();
findViewById(R.id.button1).setOnClickListener( new OnClickListener()
{
@Override
public void onClick(View v)
{
data.putExtra( "btnone" , "name" );
setResult(RESULT_CODE_ONE, data ); //设置返回码
finish();
}
});
findViewById(R.id.button2).setOnClickListener( new OnClickListener()
{
@Override
public void onClick(View v)
{
data.putExtra( "btntwo" , 12 );
setResult(RESULT_CODE_TWO, data ); //设置返回码
finish();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.second, menu);
return true ;
}
} |
第二页按钮事件重构过程一:
1.实现OnClickListener接口
2.重写onClick(View v)
3.findViewById(R.id.button1).setOnClickListener(this)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
public class SecondActivity extends Activity implements OnClickListener //实现监听者接口
{ private final static int RESULT_CODE_ONE = 1 ;
private final static int RESULT_CODE_TWO = 2 ;
@Override
public void onClick(View v) //重写点击方法
{
Intent data = new Intent();
if (v.getId() == R.id.button1) //判断view的id
{
data.putExtra( "btnone" , "name" );
setResult(RESULT_CODE_ONE, data );
finish();
}
if (v.getId() == R.id.button2)
{
data.putExtra( "btntwo" , 12 );
setResult(RESULT_CODE_TWO, data );
finish();
}
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
findViewById(R.id.button1).setOnClickListener( this ); //监听按钮
findViewById(R.id.button2).setOnClickListener( this );
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.second, menu);
return true ;
}
} |
第二页按钮事件重构过程二:将事件内容以方法代替
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
public class SecondActivity extends Activity implements OnClickListener //实现监听者接口
{ private final static int RESULT_CODE_ONE = 1 ;
private final static int RESULT_CODE_TWO = 2 ;
@Override
public void onClick(View v) //重写点击方法
{
Intent data = new Intent();
if (v.getId() == R.id.button1) //判断view的id
{
// data.putExtra("btnone", "name"); // setResult(RESULT_CODE_ONE, data ); // finish(); sendResult( "btnone" , "name" , RESULT_CODE_ONE);
}
if (v.getId() == R.id.button2)
{
// data.putExtra("btntwo", 12); // setResult(RESULT_CODE_TWO, data ); // finish(); sendResult( "btntwo" , Integer.valueOf( 12 ), RESULT_CODE_TWO);
}
}
private void sendResult(String extraKey, Object content, int resultCode)
{
Intent data = new Intent();
if (content instanceof String)
{
data.putExtra(extraKey, (String) content);
}
else if (content instanceof Integer)
{
data.putExtra(extraKey, (Integer) content);
}
setResult(resultCode, data);
finish();
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
findViewById(R.id.button1).setOnClickListener( this ); //监听按钮
findViewById(R.id.button2).setOnClickListener( this );
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.second, menu);
return true ;
}
}
|
本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1198584,如需转载请自行联系原作者