标签:
直接上代码吧。注意导入apache commons-codec的jar包。
1、布局文件
<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="com.example.hellom.MainActivity$PlaceholderFragment" > <TextView android:id="@+id/notic" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello_world" /> <TextView android: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" /> <EditText android: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> <TextView android: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" /> <TextView android: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="" /> <TextView android: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" /> <TextView android: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="" /> <Button android: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" /> <Button android: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
@Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (savedInstanceState == null)
        {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
    } 
3、fragment
public class PlaceholderFragment extends Fragment
{
    //明文
    public static EditText text01;
    //密文
    public static TextView text02;
    //测试明文
    public static TextView text03;
    //加密事件按钮
    public static Button translationBtn01;
    //解密事件按钮
    public static Button translationBtn02;
    
    public PlaceholderFragment()
    {
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)
    {
        View rootView = 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);
        return rootView;
    }
    @Override
    public void onAttach(Activity activity)
    {
        // TODO Auto-generated method stub
        super.onAttach(activity);
    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        
        translationBtn01.setOnClickListener(new BtnOnClickListener(getActivity()));
        translationBtn02.setOnClickListener(new BtnOnClickListener(getActivity()));
    }
    
    
} 
3、fragment中的按钮事件类(独立类的缺点是需要较多的static变量)
public class BtnOnClickListener implements OnClickListener
{
    private Context context;
    public BtnOnClickListener(Context context)
    {
        // TODO Auto-generated constructor stub
        this.context = context;
    }
    @Override
    public void onClick(View v)
    {
        // TODO Auto-generated method stub
        int id = v.getId();
        switch (id)
        {
        // 加密按钮事件按钮
        case R.id.translation01:
        {
            if (TextUtils.isEmpty(PlaceholderFragment.text01.getText()))
            {
                Toast.makeText(context, "请输入需要加密的内容", Toast.LENGTH_SHORT)
                        .show();
            } else
            {
                String text = PlaceholderFragment.text01.getText().toString()
                        .trim();
                Toast.makeText(context, "开始加密", Toast.LENGTH_SHORT).show();
                String text01 = new String(Hex.encodeHex(DigestUtils.md5(text)));
                PlaceholderFragment.text02.setText(text01);
                PlaceholderFragment.translationBtn02.setEnabled(true);
                PlaceholderFragment.translationBtn01.setEnabled(false);
            }
        }
            break;
        case R.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;
        }
    }
} 
标签:
原文地址:http://my.oschina.net/AaronCN/blog/401602