【发布时间】:2016-08-20 00:46:34
【问题描述】:
我正在尝试像在 Windows 论坛中那样单击按钮,但是我做错了,因为没有任何内容写入控制台,并且按钮的文本没有改变。为了测试目的,我更改了按钮文本。 我也知道我连接到 SQL 数据库的方法是不安全的,但是这个应用程序是严格供个人使用的,并且现在是学习目的。无论如何,这是我的代码:
Main.axml 代码:
<Button
android:text="Login"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="15"
android:background="@drawable/buttonstyle"
android:id="@+id/buttonLogIn"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:drawableLeft="@drawable/lockicon"
android:textStyle="bold"
android:textColor="#FFFFFF" />
MainActivity.cs 代码:
using System;
using Android.App;
using Android.Widget;
using Android.OS;
using CryptSharp;
using MySql.Data.MySqlClient;
using System.Data;
namespace App1
{
[Activity(Label = "TexByte", MainLauncher = true, Theme = "@android:style/Theme.Black.NoTitleBar.Fullscreen", Icon = "@drawable/Logo_Mob2")]
public class MainActivity : Activity
{
private EditText mtxtUsername, mtxtPassword;
private Button mBtnSignIn;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
mBtnSignIn = FindViewById<Button>(Resource.Id.buttonLogIn);
mtxtUsername = FindViewById<EditText>(Resource.Id.txtUsername);
mtxtPassword = FindViewById<EditText>(Resource.Id.txtPassword);
}
private void mBtnSignIn_Click(object sender, EventArgs args)
{
MySqlConnection con = new MySqlConnection("Server=127.0.0.1;User Id=root;Password=password;Database=login;");
try
{
string username = mtxtUsername.Text;
string password = mtxtPassword.Text;
if (con.State == ConnectionState.Closed)
{
con.Open();
MySqlCommand cmd = new MySqlCommand("SELECT * FROM members WHERE username = '" + username + "' order by password ");
cmd.CommandType = CommandType.Text;
MySqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
string hash = string.Format("{0}", rdr["password"]);
Console.WriteLine(hash);
if (Crypter.CheckPassword(password, hash))
{
Console.WriteLine("User name and Password Success ");
mBtnSignIn.Text = "Test";
}
else
{
Console.WriteLine("Unable to process request. Verify username and password are correct.");
mBtnSignIn.Text = "Fail";
}
}
}
}
catch(MySqlException ex)
{
Console.WriteLine("ERROR: Something went wrong. :( ", ex.Message);
}
finally
{
con.Close();
}
}
}
}
【问题讨论】:
-
您在哪里向您的
Button添加事件处理程序?我只是回忆以前的 C# + .Net 经验。 -
如果我没记错的话,应该是这个吧? mBtnSignIn = FindViewById
-
这只是找到按钮对象。您的按钮对象需要告诉它应该调用哪个代码来处理事件,例如单击。在教程和代码示例中查看它是如何完成的。
标签: c# android visual-studio xamarin