Skip to content

Commit

Permalink
完成android版的界面及功能
Browse files Browse the repository at this point in the history
修改wpf中simplekey提交时的赋值逻辑及bug
  • Loading branch information
Tar-Palantir committed Mar 22, 2016
1 parent 2cdbd6d commit 2b54e80
Show file tree
Hide file tree
Showing 13 changed files with 748 additions and 137 deletions.
247 changes: 247 additions & 0 deletions PasswordGenerator/PasswordGenerator.Android/Activities/MainActivity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
using System;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Views;
using Android.Widget;
using Java.Lang;
using PasswordGenerator.Core;
using PasswordGenerator.Core.Enums;
using PasswordGenerator.Core.Models;

namespace PasswordGenerator.Android.Activities
{
[Activity(Label = "PasswordGenerator", MainLauncher = true, Icon = "@drawable/icon")]
public partial class MainActivity : Activity
{
private ClipboardManager _clipboardManager;

private bool _initCompleted;

private readonly ToggleButton[] _signalArray = new ToggleButton[12];

protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);

// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);

Definition();
Initialize();

RefreshModes();
}

private void Initialize()
{
_clipboardManager = (ClipboardManager)GetSystemService(ClipboardService);

_btnGenerate.Click += btnGenerate_Click;
_btnSubmit.Click += btnSubmit_Click;
_lytOptionExpand.Click += lytOptionExpand_Click;
_btnCopy.Click += btnCopy_Click;

_cbUpper.Click += (o, e) =>
{
_rGroupUpper.Visibility = _cbUpper.Checked ? ViewStates.Visible : ViewStates.Gone;
RefreshModes();
};
_cbLower.Click += (o, e) =>
{
_rGroupLower.Visibility = _cbLower.Checked ? ViewStates.Visible : ViewStates.Gone;
RefreshModes();
};
_cbNum.Click += (o, e) =>
{
_rGroupNum.Visibility = _cbNum.Checked ? ViewStates.Visible : ViewStates.Gone;
RefreshModes();
};
_cbSignals.Click += (o, e) =>
{
_rGroupSignals.Visibility = _cbSignals.Checked ? ViewStates.Visible : ViewStates.Gone;
_tlytSignals.Visibility = _cbSignals.Checked ? ViewStates.Visible : ViewStates.Gone;
RefreshModes();
};
_rGroupUpper.CheckedChange += (o, e) => { RefreshModes(); };
_rGroupLower.CheckedChange += (o, e) => { RefreshModes(); };
_rGroupNum.CheckedChange += (o, e) => { RefreshModes(); };
_rGroupSignals.CheckedChange += (o, e) => { RefreshModes(); };

InitSignalList();


_initCompleted = true;
}

private void InitSignalList()
{
var signalIds = new[] {Resource.Id.tbtn1,
Resource.Id.tbtn2,Resource.Id.tbtn3,Resource.Id.tbtn4,
Resource.Id.tbtn5,Resource.Id.tbtn6,Resource.Id.tbtn7,
Resource.Id.tbtn8,Resource.Id.tbtn9,Resource.Id.tbtn10,
Resource.Id.tbtn11,Resource.Id.tbtn12};

for (int i = 0; i < 12; i++)
{
var btn = FindViewById<ToggleButton>(signalIds[i]);
btn.Click += (o, e) => { RefreshModes(); };
_signalArray[i] = btn;
}
}

private void btnGenerate_Click(object sender, EventArgs e)
{
var keywords = _txtKeywords.Text;
var length = int.Parse(_txtLength.Text);
var key = _tvSimpleKey.Text;

_tvResult.Text = Generator.Generate(keywords, length, key);
}

private void btnSubmit_Click(object sender, EventArgs e)
{
var modeStateHex = _txtSimpleKey.Text;
if (string.IsNullOrEmpty(modeStateHex)) return;

var modeStateOct = Generator.ModeStateHexToOct(modeStateHex);
var modeStates = Generator.GetModesFromOct(modeStateOct);

//将输入的模式简码转换到当前选项中
foreach (var modeState in modeStates)
{
switch (modeState.RangeType)
{
case EnumValueRangeType.UpperWord:
_rGroupUpper.Check(modeState.State == EnumChooseState.Must ? Resource.Id.rbUpper_Must : Resource.Id.rbUpper_Optional);
_cbUpper.Checked = modeState.State != EnumChooseState.None;
_rGroupUpper.Visibility = _cbUpper.Checked ? ViewStates.Visible : ViewStates.Gone;
break;
case EnumValueRangeType.LowerWord:
_rGroupLower.Check(modeState.State == EnumChooseState.Must ? Resource.Id.rbLower_Must : Resource.Id.rbLower_Optional);
_cbLower.Checked = modeState.State != EnumChooseState.None;
_rGroupLower.Visibility = _cbLower.Checked ? ViewStates.Visible : ViewStates.Gone;
break;
case EnumValueRangeType.Number:
_rGroupNum.Check(modeState.State == EnumChooseState.Must ? Resource.Id.rbNum_Must : Resource.Id.rbNum_Optional);
_cbNum.Checked = modeState.State != EnumChooseState.None;
_rGroupNum.Visibility = _cbNum.Checked ? ViewStates.Visible : ViewStates.Gone;
break;
case EnumValueRangeType.Signal:
_rGroupSignals.Check(modeState.State == EnumChooseState.Must ? Resource.Id.rbSignals_Must : Resource.Id.rbSignals_Optional);
_cbSignals.Checked = modeState.State != EnumChooseState.None;
_tlytSignals.Visibility = _rGroupSignals.Visibility = _cbSignals.Checked ? ViewStates.Visible : ViewStates.Gone;

var chosenSignals = Generator.GetSignalsFromOct(modeStateOct);
foreach (ToggleButton child in _signalArray)
{
child.Checked = chosenSignals.Contains(child.Text);
}
break;
default:
break;
}
}

_tvSimpleKey.Text = _txtSimpleKey.Text;
}

private void lytOptionExpand_Click(object sender, EventArgs e)
{
var lytOptions = FindViewById<ViewGroup>(Resource.Id.lytOptions);
var imgOptions = FindViewById<ImageView>(Resource.Id.imgOptions);
if (lytOptions.Visibility == ViewStates.Visible)
{
lytOptions.Visibility = ViewStates.Gone;
imgOptions.SetImageResource(Resource.Drawable.Expand);
}
else
{
lytOptions.Visibility = ViewStates.Visible;
imgOptions.SetImageResource(Resource.Drawable.Collapse);
}
}

private void btnCopy_Click(object sender, EventArgs e)
{
var data = ClipData.NewPlainText("text", _tvResult.Text);
_clipboardManager.PrimaryClip = data;

var dialog = new AlertDialog.Builder(this).Create();
dialog.SetTitle("成功");
dialog.SetMessage("复制成功");
dialog.SetCanceledOnTouchOutside(true);
dialog.Show();
}


#region 私有方法

/// <summary>
/// 刷新当前模式
/// </summary>
private void RefreshModes()
{
if (!_initCompleted) return;

StringBuilder keySBuilder = new StringBuilder();
GetModeState(_cbUpper, _rGroupUpper, keySBuilder);
GetModeState(_cbLower, _rGroupLower, keySBuilder);
GetModeState(_cbNum, _rGroupNum, keySBuilder);
GetModeState(_cbSignals, _rGroupSignals, keySBuilder);

//子符号的状态获取
char[] signals = { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0' };
if (_cbSignals.Checked)
{
foreach (ToggleButton child in _signalArray)
{
var index = Key.Signals.IndexOf(child.Text, StringComparison.Ordinal);
if (child.Checked)
{
signals[index] = '1';
}
}
}
keySBuilder.Append(signals);

_tvSimpleKey.Text = Generator.ModeStateOctToHex(keySBuilder.ToString());
}

/// <summary>
/// 获取模式状态
/// </summary>
/// <param name="cb">启用控制的复选框</param>
/// <param name="rGroup">可选和必选所在的容器</param>
/// <param name="sb">输出字符串构建器</param>
private void GetModeState(CheckBox cb, RadioGroup rGroup, StringBuilder sb)
{
var state = EnumChooseState.None;
if (cb.Checked)
{
var cbOption = FindViewById<RadioButton>(rGroup.CheckedRadioButtonId);
//第一个选项是“必须”选项,如果不是则不正常,默认按可选
if (rGroup.CheckedRadioButtonId != -1 && cbOption.Text == "Must")
{
state = EnumChooseState.Must;
}
else
{
state = EnumChooseState.Optional;
}
}

//保证输出长度为两位
var octStr = Convert.ToString((short)state, 2);
if (octStr.Length < 2)
{
sb.Append("0");
}
sb.Append(octStr);
}

#endregion
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using Android.Views;
using Android.Widget;

namespace PasswordGenerator.Android.Activities
{
public partial class MainActivity
{

private Button _btnGenerate;
private Button _btnSubmit;
private Button _btnCopy;

private ViewGroup _lytOptionExpand;
private TableLayout _tlytSignals;

private TextView _tvResult;
private TextView _tvSimpleKey;

private CheckBox _cbUpper;
private CheckBox _cbLower;
private CheckBox _cbNum;
private CheckBox _cbSignals;

private RadioGroup _rGroupUpper;
private RadioGroup _rGroupLower;
private RadioGroup _rGroupNum;
private RadioGroup _rGroupSignals;

private EditText _txtKeywords;
private EditText _txtLength;
private EditText _txtSimpleKey;

private void Definition()
{
_btnGenerate = FindViewById<Button>(Resource.Id.btnGenerate);
_btnSubmit = FindViewById<Button>(Resource.Id.btnSubmit);
_btnCopy = FindViewById<Button>(Resource.Id.btnCopy);

_tvResult = FindViewById<TextView>(Resource.Id.tvResult);
_tvSimpleKey = FindViewById<TextView>(Resource.Id.tvSimpleKey);

_lytOptionExpand = FindViewById<ViewGroup>(Resource.Id.lytOptionExpand);
_tlytSignals = FindViewById<TableLayout>(Resource.Id.tlytSignals);

_cbUpper = FindViewById<CheckBox>(Resource.Id.cbUpper);
_cbLower = FindViewById<CheckBox>(Resource.Id.cbLower);
_cbNum = FindViewById<CheckBox>(Resource.Id.cbNum);
_cbSignals = FindViewById<CheckBox>(Resource.Id.cbSignals);

_rGroupUpper = FindViewById<RadioGroup>(Resource.Id.rGroupUpper);
_rGroupLower = FindViewById<RadioGroup>(Resource.Id.rGroupLower);
_rGroupNum = FindViewById<RadioGroup>(Resource.Id.rGroupNum);
_rGroupSignals = FindViewById<RadioGroup>(Resource.Id.rGroupSignals);

_txtKeywords = FindViewById<EditText>(Resource.Id.txtKeywords);
_txtLength = FindViewById<EditText>(Resource.Id.txtLength);
_txtSimpleKey = FindViewById<EditText>(Resource.Id.txtSimpleKey);
}
}
}
24 changes: 0 additions & 24 deletions PasswordGenerator/PasswordGenerator.Android/MainActivity.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<AndroidResgenFile>Resources\Resource.Designer.cs</AndroidResgenFile>
<GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
<AndroidUseLatestPlatformSdk>False</AndroidUseLatestPlatformSdk>
<TargetFrameworkVersion>v4.0.3</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.4</TargetFrameworkVersion>
<AndroidManifest>Properties\AndroidManifest.xml</AndroidManifest>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
Expand All @@ -30,10 +30,11 @@
<AndroidUseSharedRuntime>True</AndroidUseSharedRuntime>
<AndroidLinkMode>None</AndroidLinkMode>
<AndroidLinkSkip />
<EmbedAssembliesIntoApk>True</EmbedAssembliesIntoApk>
<BundleAssemblies>True</BundleAssemblies>
<EmbedAssembliesIntoApk>False</EmbedAssembliesIntoApk>
<BundleAssemblies>False</BundleAssemblies>
<AndroidCreatePackagePerAbi>False</AndroidCreatePackagePerAbi>
<AndroidSupportedAbis>armeabi,armeabi-v7a,x86,x86_64,arm64-v8a</AndroidSupportedAbis>
<AndroidSupportedAbis>
</AndroidSupportedAbis>
<AndroidStoreUncompressedFileExtensions />
<MandroidI18n />
<Debugger>Xamarin</Debugger>
Expand All @@ -49,6 +50,19 @@
<WarningLevel>4</WarningLevel>
<AndroidUseSharedRuntime>False</AndroidUseSharedRuntime>
<AndroidLinkMode>SdkOnly</AndroidLinkMode>
<AndroidLinkSkip />
<EmbedAssembliesIntoApk>True</EmbedAssembliesIntoApk>
<BundleAssemblies>False</BundleAssemblies>
<AndroidCreatePackagePerAbi>False</AndroidCreatePackagePerAbi>
<AndroidSupportedAbis>armeabi,armeabi-v7a,x86,x86_64,arm64-v8a</AndroidSupportedAbis>
<AndroidStoreUncompressedFileExtensions />
<MandroidI18n />
<Debugger>Microsoft</Debugger>
<AotAssemblies>False</AotAssemblies>
<EnableLLVM>False</EnableLLVM>
<AndroidEnableMultiDex>False</AndroidEnableMultiDex>
<EnableProguard>False</EnableProguard>
<DevInstrumentationEnabled>False</DevInstrumentationEnabled>
</PropertyGroup>
<ItemGroup>
<Reference Include="Mono.Android" />
Expand All @@ -67,7 +81,8 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="MainActivity.cs" />
<Compile Include="Activities\MainActivity.cs" />
<Compile Include="Activities\MainActivity_Definition.cs" />
<Compile Include="Resources\Resource.Designer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down Expand Up @@ -103,6 +118,9 @@
<Name>PasswordGenerator.Core</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<AndroidResource Include="Resources\drawable\rect_frame_gray.xml" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
Loading

0 comments on commit 2b54e80

Please sign in to comment.