我们多次在Android或Java中遇到以下代码或类似内容。这段代码似乎包含重复,这根本不是一个好习惯。必须有一些更好的方法来做到这一点。是否有更短的代码来实现这一目标?
<script src="https://code.jquery.com/jquery-1.12.2.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.0-rc.1/jquery-ui.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet"/>
<body>
<div id="mask"></div>
<div>
<h3>this is new html document generated by newject extention</h3>
</div>
<div class="clearfix" data-nitstextbutton style="display: none;">
<a href="#nitstexteditor" class="btn btn-circle btn-sm default">
Edit <i class="fa fa-pencil"></i>
</a>
<a href="#" class="btn btn-icon-only btn-circle default">
<i class="fa fa-circle-o-notch"></i>
</a>
</div>
<div id="editable" style ="top: 50%; left: 50%" data-nitspagelabel="1" data-nitslabeltype="text"><h3>This is the content editable where I can change fonts and edit text</h3></div>
<div id="nitstexteditor" style="display: none;" data-nitstextpopup="0" data-nitseditor="1">
<div class="portlet box blue">
<div class="portlet-title">
<div class="caption">
<i class="fa fa-pencil-square-o"></i> Text Editor
</div>
<div class="tools">
<a href="" class="remove">
</a>
</div>
</div>
<div class="portlet-body form">
<form role="form">
<div class="form-body">
<div class="form-group">
<div class="input-group">
<span class="input-group-addon input-circle-left">
<i class="fa fa-magic"></i>
</span>
<input type="text" class="form-control input-circle-right" placeholder="Select style">
</div>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon input-circle-left">
<i class="fa fa-font"></i>
</span>
<input type="text" class="form-control input-circle-right" placeholder="Select font">
</div>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon input-circle-left">
<i class="fa fa-font"></i>
</span>
<input type="text" class="form-control input-circle-right" placeholder="Select Size">
</div>
</div>
<div class="form-group">
<a href="javascript:;" class="btn btn-icon-only default">
<i class="fa fa-bold"></i>
</a>
<a href="javascript:;" class="btn btn-icon-only default">
<i class="fa fa-italic"></i>
</a>
<a href="javascript:;" class="btn btn-icon-only default">
<i class="fa fa-underline"></i>
</a>
<a href="javascript:;" class="btn btn-icon-only default">
<i class="fa fa-align-justify"></i>
</a>
<a href="javascript:;" class="btn btn-icon-only default">
<i class="fa fa-align-left"></i>
</a>
</div>
<div class="form-group">
<a href="javascript:;" class="btn btn-icon-only default">
<i class="fa fa-align-right"></i>
</a>
<a href="javascript:;" class="btn btn-icon-only default">
<i class="fa fa-undo"></i>
</a>
<a href="javascript:;" class="btn btn-icon-only default">
<i class="fa fa-repeat"></i>
</a>
<a href="javascript:;" class="btn btn-icon-only default">
<i class="fa fa-link"></i>
</a>
<a href="javascript:;" class="btn btn-icon-only default">
<i class="fa fa-chain-broken"></i>
</a>
</div>
</div>
</form>
</div>
</div>
</div>
</body>
答案 0 :(得分:2)
在静态初始化器或构造函数中为活动类创建一个id表:
HashMap<Integer, Class<?>> map = new HashMap<>();
map.put(R.id.foo, Foo.class); // repeat for each id/class pair
然后使用地图而不是开关:
startActivity(new Intent(this), map.get(v.getId()));
答案 1 :(得分:1)
正如我评论的那样使用setClass
方法。这样做。
将您的Activity
类名设置为按钮标记,并在button
点击时获取此标记。
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="YourActivityName"
/>
Java代码
String classname = (String) textView.getTag();
intent.setClassName(getPackageName().toString(), classname)
startActivity(intent);
答案 2 :(得分:0)
是的当然。这根本不是一个好习惯。尝试实现泛化。
以下是我付诸实践的内容。
只是一个想法,为什么我会再次编写代码,当它已经写好了!! !!
这样思考,
将所有常用代码放入公共类
扩展该课程
使用其成员
创建一个类,如下所示BaseAppCompatActivity.java
:
public abstract class BaseAppCompatActivity extends AppCompatActivity {
private ProgressDialog mProgressDialog;
private Toolbar mToolbar;
private TabLayout mTabLayout;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutResourceId());
initiateViews();
}
protected abstract int getLayoutResourceId();
protected abstract void initiateViews();
public void setHasToolBar() {
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
}
public Toolbar getToolBar() {
return mToolbar;
}
public void setHasTabLayout() {
mTabLayout = (TabLayout) findViewById(R.id.tabLayout);
}
public TabLayout getTabLayout() {
return mTabLayout;
}
public void showProgressDialog() {
showProgressDialog("Please Wait ...");
}
public void showProgressDialog(String message) {
// progress bar not null and is visible, so set the title
if (mProgressDialog != null && mProgressDialog.isShowing()) {
mProgressDialog.setMessage(message);
}
// create new progress bar
else {
mProgressDialog = ProgressDialog.show(this, "Loading", message, true, false);
}
}
public void hideProgressDialog() {
if (mProgressDialog != null && mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
}
}
public void showAlertDialog(String message) {
AlertDialog alertDialog = new AlertDialog.Builder(this)
.setNeutralButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
})
.create();
alertDialog.setMessage(message);
alertDialog.show();
}
public void showToast(String message) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
public void logInformation(Class tag, String description) {
Log.i(tag.getName(), description);
}
public void logError(Class tag, String description) {
Log.e(tag.getName(), description);
}
public void logDebug(Class tag, String description) {
Log.d(tag.getName(), description);
}
public void launchActivity(Class<? extends Activity> cls, Bundle bundle, int code) {
Intent intent = new Intent();
if (bundle != null)
intent.putExtras(bundle);
intent.setClass(activity, cls);
if (code == -1)
startActivity(intent);
else
startActivityForResult(intent, code);
}
public void loadFragment(Class fragmentClass, boolean addToBackStack) {
Fragment fragment = null;
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
if (fragment != null) {
if (addToBackStack) {
getSupportFragmentManager()
.beginTransaction()
.addToBackStack(fragmentClass.getSimpleName())
.replace(R.id.frameLayout, fragment, fragmentClass.getSimpleName())
.commit();
} else {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.frameLayout, fragment, fragmentClass.getSimpleName())
.commit();
}
}
}
}
然后,将所有活动扩展到此BaseAppCompatActivity
课程。
然后,在您的代码中,活动被简化为
launchActivity(Activity2.class, null, -1);
答案 3 :(得分:0)
您可以将代码设置为与您的活动相同:
<Button
android:id="@+id/btn1"
android:tag="DetailsActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dnt want this text" />
<Button
android:id="@+id/btn2"
android:tag="ApplyActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dnt want this text" />
<Button
android:id="@+id/btn3"
android:tag="EditActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dnt want this text" />
<Button
android:id="@+id/btn4"
android:tag="EditActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dnt want this text" />
<Button
android:id="@+id/btn5"
android:tag="DetailsActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dnt want this text" />
<Button
android:id="@+id/btn6"
android:tag="CallActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dnt want this text" />
Java代码:您的包名:com.example.pkg1
Intent intent = new Intent();
intent.setClassName("com.example.pkg1", "com.example.pkg1."+v.getTag() );
startActivity(intent);