直接上代码吧。注意导入apache commons-codec的jar包。

1、布局文件

<RelativeLayoutxmlns: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="com.example.hellom.MainActivity$PlaceholderFragment"><TextViewandroid:id="@+id/notic"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/hello_world"/><TextViewandroid:id="@+id/notic01"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignLeft="@+id/notic"android:layout_below="@+id/notic"android:layout_marginTop="24dp"android:text="@string/notic01"/><EditTextandroid:id="@+id/textIn"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignBaseline="@+id/notic01"android:layout_alignBottom="@+id/notic01"android:layout_marginLeft="19dp"android:layout_toRightOf="@+id/notic01"android:ems="10"><requestFocus/></EditText><TextViewandroid:id="@+id/ciphertext"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignLeft="@+id/notic01"android:layout_below="@+id/textIn"android:layout_marginTop="33dp"android:text="@string/notic02"/><TextViewandroid:id="@+id/ciphertext01"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignBaseline="@+id/ciphertext"android:layout_alignBottom="@+id/ciphertext"android:layout_alignLeft="@+id/textIn"android:layout_alignRight="@+id/textIn"android:text=""/><TextViewandroid:id="@+id/textOut"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_below="@+id/ciphertext"android:layout_marginTop="23dp"android:text="@string/notic04"/><TextViewandroid:id="@+id/text03"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignBottom="@+id/textOut"android:layout_alignLeft="@+id/ciphertext01"android:layout_alignRight="@+id/ciphertext01"android:text=""/><Buttonandroid:id="@+id/translation01"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignLeft="@+id/textOut"android:layout_alignParentBottom="true"android:layout_marginBottom="97dp"android:layout_marginLeft="16dp"android:text="@string/notic03"/><Buttonandroid:id="@+id/translation02"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignBottom="@+id/translation01"android:layout_alignRight="@+id/text03"android:layout_marginRight="37dp"android:text="@string/notic05"/></RelativeLayout>

2、mainActivity

@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);if(savedInstanceState==null){getFragmentManager().beginTransaction().add(R.id.container,newPlaceholderFragment()).commit();}}

3、fragment

publicclassPlaceholderFragmentextendsFragment{//明文publicstaticEditTexttext01;//密文publicstaticTextViewtext02;//测试明文publicstaticTextViewtext03;//加密事件按钮publicstaticButtontranslationBtn01;//解密事件按钮publicstaticButtontranslationBtn02;publicPlaceholderFragment(){}@OverridepublicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,BundlesavedInstanceState){ViewrootView=inflater.inflate(R.layout.fragment_main,container,false);text01=(EditText)rootView.findViewById(R.id.textIn);text02=(TextView)rootView.findViewById(R.id.ciphertext01);text03=(TextView)rootView.findViewById(R.id.text03);translationBtn01=(Button)rootView.findViewById(R.id.translation01);translationBtn02=(Button)rootView.findViewById(R.id.translation02);translationBtn02.setEnabled(false);returnrootView;}@OverridepublicvoidonAttach(Activityactivity){//TODOAuto-generatedmethodstubsuper.onAttach(activity);}@OverridepublicvoidonActivityCreated(BundlesavedInstanceState){//TODOAuto-generatedmethodstubsuper.onActivityCreated(savedInstanceState);translationBtn01.setOnClickListener(newBtnOnClickListener(getActivity()));translationBtn02.setOnClickListener(newBtnOnClickListener(getActivity()));}}

3、fragment中的按钮事件类(独立类的缺点是需要较多的static变量)

publicclassBtnOnClickListenerimplementsOnClickListener{privateContextcontext;publicBtnOnClickListener(Contextcontext){//TODOAuto-generatedconstructorstubthis.context=context;}@OverridepublicvoidonClick(Viewv){//TODOAuto-generatedmethodstubintid=v.getId();switch(id){//加密按钮事件按钮caseR.id.translation01:{if(TextUtils.isEmpty(PlaceholderFragment.text01.getText())){Toast.makeText(context,"请输入需要加密的内容",Toast.LENGTH_SHORT).show();}else{Stringtext=PlaceholderFragment.text01.getText().toString().trim();Toast.makeText(context,"开始加密",Toast.LENGTH_SHORT).show();Stringtext01=newString(Hex.encodeHex(DigestUtils.md5(text)));PlaceholderFragment.text02.setText(text01);PlaceholderFragment.translationBtn02.setEnabled(true);PlaceholderFragment.translationBtn01.setEnabled(false);}}break;caseR.id.translation02:{Toast.makeText(context,"开始解密",Toast.LENGTH_SHORT).show();PlaceholderFragment.text03.setText("MD5为不可逆加密,可尝试Base64.");PlaceholderFragment.translationBtn02.setEnabled(false);PlaceholderFragment.translationBtn01.setEnabled(true);}break;default:break;}}}


更多相关文章

  1. 2010.12.22——— android 一个永远处于底部的按钮集合
  2. Android事件处理
  3. Android之各种事件触发方案
  4. Android圆形水波纹按钮的实现(Ripple)
  5. 「Android 进阶」Android 事件分发机制
  6. android开发_文本按钮 与 输入框
  7. title上左右按钮
  8. checkbox 与listview 点击事件冲突
  9. Android实现输入法弹出时把布局顶上去和登录按钮顶上去的解决方

随机推荐

  1. Android下的单元测试
  2. Android中的MVP架构初探
  3. Android将允许纯C/C++开发应用
  4. Android(安卓)NDK环境配置与开发
  5. Android的一点思考
  6. Android撬动IT市场的新支点
  7. android模拟器大幅优化_android开发者的
  8. [置顶] Android(安卓)webview开发中 js与
  9. Android将允许纯C/C++开发应用
  10. Android学习之路(一)之 Android文件简单介