android中的代码重用 [英] Code reuse in android

查看:64
本文介绍了android中的代码重用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在命名的first_activity和second_activity中有2个布局...

first_activity有一个按钮button1,我已经应用了onclick监听器来制作一个简单的toast,我已经使用了这个first_activity FirstActivity ...

second_activity只有一个按钮,我在second_activity中包含了first_activity,所以second_activity有2个按钮......

现在已经编码了使用FirstActivity运行应用程序有主要活动,我正在获得第一个按钮的totlick ...

现在,SecondActivity将FirstActivity扩展到它自己的按钮2点击事件......

现在我已经将启动活动更改为SecondActivity并且在运行之后我得到的第二个按钮的点击事件不是第一个按钮..

所以我在哪里犯错..

我的目的是使代码重用,例如我有一个应用程序,其中有30个布局,所有布局都有共同的菜单,包含在每个布局中,所以我只想为该菜单编码一次,并在所有其他布局中重用该代码...



这里是我的应用程序的代码。 ...

first_activity:

< relativelayout xmlns:android =http://schemas.android.com/apk/res/android>

xmlns:tools =http://schemas.android.com/tools

android:layout_width =match_parent

android:layout_height = 200dip

android:paddingBottom =@ dimen / activity_vertical_margin

android:paddingLeft =@ dimen / activity_horizo​​ntal_margin

android:paddingRight = @ dimen / activity_horizo​​ntal_margin

android:paddingTop =@ dimen / activity_vertical_margin

tools:context =。FirstActivity>



<按钮

android:id =@ + id / button1

android:layout_width =wrap_content

android:layout_height =wrap_content

android:t ext =第一个按钮/>









second_activity :

< relativelayout xmlns:android =http://schemas.android.com/apk/res/android>

xmlns:tools =http: //schemas.android.com/tools

android:layout_width =match_parent

android:layout_height =match_parent

android: paddingBottom =@ dimen / activity_vertical_margin

android:paddingLeft =@ dimen / activity_horizo​​ntal_margin

android:paddingRight =@ dimen / activity_horizo​​ntal_margin

android:paddingTop =@ dimen / activity_vertical_margin

工具:context =。FirstActivity>



< include layout =@ layout / first_activity>

android:id =@ + id / layout/>



< Button

android:id =@ + id / button2

android:layout_width =wrap_content

android:layout_height =wr ap_content

android:layout_below =@ id / layout

android:text =2nd Button/>









FirstActivity:

package com.example.codereuse;



导入android.os.Bundle;

导入android.app.Activity;

导入android.view.Menu;

导入android.view.View;

导入android.view.View.OnClickListener;

导入android.widget.Button;

import android.widget.Toast;



公共类FirstActivity扩展活动{



Button b1;



@Override

protected void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState) ;

setContentView(R.layout.first_activity);



b1 =(按钮)findViewById(R.id.button1);



b1.setOnClickListener(new OnClickListener(){



@Override

public void onClick(查看v){

// TODO自动生成方法存根

Toast.makeText(getApplicationContext (),按钮1单击,Toast.LENGTH_LONG)。show();

}

});

}



@Override

public boolean onCreateOptionsMenu(菜单菜单){

//给菜单充气;如果它存在,这会将项目添加到操作栏。

getMenuInflater()。inflate(R.menu.main,menu);

返回true;

}





}





SecondActivity:

包com.example.codereuse;



导入android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

import android.view.View;

import android。 view.View.OnClickListener;

import android.widget.Button;

import android.widget.Toast;



公共类SecondActivity扩展FirstActivity {



按钮b2;



@Override

protected void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.second_activity);



b2 =(按钮)findViewById(R.id.button2);



b2.setOnCli ckListener(新的OnClickListener(){



@Override

public void onClick(查看v){

/ / TODO自动生成的方法存根

Toast.makeText(getApplicationContext(),按钮2单击,Toast.LENGTH_LONG)。show();

}

});

}



@Override

public boolean onCreateOptionsMenu(菜单菜单) {

//给菜单充气;如果它存在,这会将项目添加到操作栏。

getMenuInflater()。inflate(R.menu.main,menu);

返回true;

}



}





AndroidMenifest:



< manifest xmlns:android =http://schemas.android.com/apk/res/android>

package = com.example.codereuse

android:versionCode =1

android:versionName =1.0>



< uses-sdk>

android:minSdkVersion =8

android:targetSdkVersion =17/>



< application>

android:allowBackup =true

android:icon =@ drawable / ic_launcher

android:label =@ string / app_name

android:theme =@ style / AppTheme>

< activity>

android:name =com.example.codereuse.SecondActivity

androi d:label =@ string / app_name>

< intent-filter>

< action android:name =android.intent.action.MAIN >



< category android:name =android.intent.category.LAUNCHER>







< activity>

android:name =com.example.codereuse.FirstActivity

android:label =@ string / app_name>















读者很快回复...

I have 2 layouts in named first_activity and second_activity...
the first_activity has a single button button1 on which i have applied onclick listener to make a simple toast and i have used this first_activity in the FirstActivity...
the second_activity has a single button along with that i have included the first_activity in the second_activity so the second_activity has 2 buttons...
now have coded and run the application using FirstActivity has the main activity and the i am getting the toast onclick of 1st button...
Now the SecondActivity extends FirstActivity along its own button 2 click event...
now i have changed the start activity as SecondActivity and after running i am getting the click event of only 2nd button not of 1st button..
so where i am making mistake..
my intention is to make code reuse for example i have an app in which there are 30 layout and all the layout have common menu which is included in each layout so i just want to code for that menu once and reuse that code in all the other layout...

here are the code of my app....
first_activity:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android">
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="200dip"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".FirstActivity" >

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1st Button" />




second_activity:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android">
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".FirstActivity" >

<include layout="@layout/first_activity">
android:id="@+id/layout"/>

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/layout"
android:text="2nd Button" />




FirstActivity:
package com.example.codereuse;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class FirstActivity extends Activity {

Button b1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_activity);

b1 = (Button) findViewById(R.id.button1);

b1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Button 1 Clicked", Toast.LENGTH_LONG).show();
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


}


SecondActivity:
package com.example.codereuse;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class SecondActivity extends FirstActivity {

Button b2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);

b2 = (Button) findViewById(R.id.button2);

b2.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Button 2 Clicked", Toast.LENGTH_LONG).show();
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}


AndroidMenifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
package="com.example.codereuse"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk>
android:minSdkVersion="8"
android:targetSdkVersion="17" />

<application>
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity>
android:name="com.example.codereuse.SecondActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN">

<category android:name="android.intent.category.LAUNCHER">



<activity>
android:name="com.example.codereuse.FirstActivity"
android:label="@string/app_name" >







Readers Reply very soon...

推荐答案

你好Ganeshcse ..

thanx回复..

i很遗憾地说,但你提供的链接对我不起作用..

你能给我更精确的解决方案..

我想要的是我有一个带有5个按钮的通用菜单所以我想制作一个单独的活动代码以便我可以使用所有其他活动中的代码,以便我不必一次又一次地重复该公共代码......
hello Ganeshcse..
thanx for reply..
i am sorry to say but the link that you have provided is not working for me..
can you give me more precise solution..
what i want is i have a common menu with 5 buttons so i want to make a separate activity code so that i can use that code in all other activity so that i dont have to repeat that common code again and again...


这篇关于android中的代码重用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆