C# Xamarin Android中的EditText有多清晰
单击按钮后,我想在打开焦点时清除编辑文本字段 此代码是一个sms应用程序,我想在单击“发送”按钮再次键入另一条消息后,在聚焦该EditTExt字段时清除该EditTExt字段 我试图在axml文件中放置一个gotfocus属性,但它不起作用。。 我尝试在google上搜索,但找不到任何解决方案,其他程序员使用了gotfocus属性,但我认为gotfocus属性仅在xaml文件中可用,而在axml中不可用C# Xamarin Android中的EditText有多清晰,c#,xamarin.android,C#,Xamarin.android,单击按钮后,我想在打开焦点时清除编辑文本字段 此代码是一个sms应用程序,我想在单击“发送”按钮再次键入另一条消息后,在聚焦该EditTExt字段时清除该EditTExt字段 我试图在axml文件中放置一个gotfocus属性,但它不起作用。。 我尝试在google上搜索,但找不到任何解决方案,其他程序员使用了gotfocus属性,但我认为gotfocus属性仅在xaml文件中可用,而在axml中不可用 using Android.App; using Android.OS; using And
using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Widget;
using Android.Content.PM;
using Android.Telephony;
using System;
using Android;
using Android.Content;
using Android.Support.V4.Content;
using Android.Support.V4.App;
using Android.Util;
using Android.Support.Design.Widget;
using Android.Views;
using Android.Text;
using System.Threading;
namespace MhylesOrderingApp
{
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity, ActivityCompat.IOnRequestPermissionsResultCallback
{
static readonly int REQUEST_SENDSMS = 0;
private SmsManager _smsManager;
private BroadcastReceiver _smsSentBroadcastReceiver, _smsDeliveredBroadcastReceiver;
View layout;
//private ProgressBar spinner;
static Android.App.ProgressDialog progress;
//Android.App.ProgressDialog.progress;
//public virtual ViewStates Visibility { get; set; }
//SMSSentReceiver receiver;
//private SmsManager smsManager;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
layout = FindViewById(Resource.Id.sample_main_layout);
//spinner = (ProgressBar)FindViewById(Resource.Id.progressBar1);
var smsBtn = FindViewById<Button>(Resource.Id.btnSend);
var phoneNum = FindViewById<EditText>(Resource.Id.phoneNum);
var smsDate = "";
var smsCustomer = FindViewById<EditText>(Resource.Id.txtCustomer);
var smsAddress = FindViewById<EditText>(Resource.Id.txtAddress);
var smsCustNo = FindViewById<EditText>(Resource.Id.txtCustomerNo);
var smsOrderProd = FindViewById<EditText>(Resource.Id.txtOrderedProduct);
var smsProdQty = FindViewById<EditText>(Resource.Id.txtProductQty);
var smsUnit = FindViewById<EditText>(Resource.Id.txtUnit);
var smsAgentName = FindViewById<EditText>(Resource.Id.txtAgentName);
var scrollView = FindViewById<ScrollView>(Resource.Id.scrollViewing);
DateTime now = DateTime.Now.ToLocalTime();
smsDate = now.ToString();
scrollView.SetBackgroundResource(Resource.Drawable.TextViewStyle);
phoneNum.SetBackgroundResource(Resource.Drawable.EditTextStyle);
smsCustomer.SetBackgroundResource(Resource.Drawable.EditTextStyle);
smsAddress.SetBackgroundResource(Resource.Drawable.EditTextStyle);
smsCustNo.SetBackgroundResource(Resource.Drawable.EditTextStyle);
smsOrderProd.SetBackgroundResource(Resource.Drawable.EditTextStyle);
smsProdQty.SetBackgroundResource(Resource.Drawable.EditTextStyle);
smsUnit.SetBackgroundResource(Resource.Drawable.EditTextStyle);
smsAgentName.SetBackgroundResource(Resource.Drawable.EditTextStyle);
//spinner.Visibility = (ViewStates.Gone);
progress = new Android.App.ProgressDialog(this);
progress.Indeterminate = true;
progress.SetProgressStyle(Android.App.ProgressDialogStyle.Spinner);
progress.SetMessage("Sending.. Please wait!");
progress.SetCancelable(false);
_smsManager = SmsManager.Default;
//receiver = new SMSSentReceiver();
//smsManager = SmsManager.Default;
//IntentFilter intentFilter = new IntentFilter("SMS_SENT");
//intentFilter.AddAction("android.permission.SEND_SMS");
////RegisterReceiver(receiver, intentFilter);
//smsBtn.Click += (s, e) =>
//{
// var phone = phoneNum.Text;
// var message = sms.Text;
// var piSent = PendingIntent.GetBroadcast(this, 0, new Android.Content.Intent("SMS_SENT"), 0);
// //var piDelivered = PendingIntent.GetBroadcast(this, 0, new Android.Content.Intent("SMS_DELIVERED"), 0);
// smsManager.SendTextMessage(phone, null, message, piSent, null);
//};
smsBtn.Click += (s, e) =>
{
progress.Show();
if (TextUtils.IsEmpty(smsDate) || TextUtils.IsEmpty(phoneNum.Text) || TextUtils.IsEmpty(smsCustomer.Text)
|| TextUtils.IsEmpty(smsAddress.Text) || TextUtils.IsEmpty(smsCustNo.Text) || TextUtils.IsEmpty(smsOrderProd.Text)
|| TextUtils.IsEmpty(smsProdQty.Text) || TextUtils.IsEmpty(smsUnit.Text) || TextUtils.IsEmpty(smsAgentName.Text))
{
new Thread(new ThreadStart(delegate
{
RunOnUiThread(() => Toast.MakeText(this, "Please fill out all the fields", ToastLength.Long).Show());
RunOnUiThread(() => progress.Hide());
})).Start();
//Toast.MakeText(this, "Please fill out all the fields", ToastLength.Short).Show();
return;
}
else
{
var phone = phoneNum.Text;
var message = smsDate + "|" + smsCustomer.Text + "|" + smsAddress.Text + "|" + smsCustNo.Text
+ "|" + smsOrderProd.Text + "|" + smsProdQty.Text + "|" + smsUnit.Text + "|" + smsAgentName.Text;
var piSent = PendingIntent.GetBroadcast(this, 0, new Intent("SMS_SENT"), 0);
var piDelivered = PendingIntent.GetBroadcast(this, 0, new Intent("SMS_DELIVERED"), 0);
if ((int)Build.VERSION.SdkInt < 23)
{
//spinner.Visibility = (ViewStates.Visible);
_smsManager.SendTextMessage(phone, null, message, piSent, piDelivered);
new Thread(new ThreadStart(delegate
{
RunOnUiThread(() => Toast.MakeText(this, "Sending Orders error! please restart the app", ToastLength.Long).Show());
RunOnUiThread(() => progress.Hide());
})).Start();
//Toast.MakeText(this, "Sending Orders error! please restart the app", ToastLength.Short).Show();
return;
}
else
{
if (ActivityCompat.CheckSelfPermission(this, Manifest.Permission.SendSms) != (int)Permission.Granted)
{
// Permission is not granted. If necessary display rationale & request.
RequestSendSMSPermission();
//new Thread(new ThreadStart(delegate
//{
// //RequestSendSMSPermission();
// RunOnUiThread(() => progress.Hide());
//})).Start();
}
else
{
// We have permission, go ahead and send SMS.
//spinner.Visibility = (ViewStates.Visible);
_smsManager.SendTextMessage(phone, null, message, piSent, piDelivered);
//new Thread(new ThreadStart(delegate
//{
// //_smsManager.SendTextMessage(phone, null, message, piSent, piDelivered);
// RunOnUiThread(() => progress.Hide());
//})).Start();
}
}
}
//_smsManager.SendTextMessage(phone, null, message, piSent, piDelivered);
};
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
public void RequestSendSMSPermission()
{
Log.Info("MainActivity", "Message permission has NOT been granted. Requesting permission.");
if (ActivityCompat.ShouldShowRequestPermissionRationale(this, Manifest.Permission.SendSms))
{
Log.Info("MainActivity", "Displaying message permission rationale to provide additional context.");
Snackbar.Make(layout, "Message permission is needed to send SMS.",
Snackbar.LengthIndefinite).SetAction("OK", new Action<View>(delegate (View obj) {
ActivityCompat.RequestPermissions(this, new String[] { Manifest.Permission.SendSms }, REQUEST_SENDSMS);
})).Show();
}
else
{
ActivityCompat.RequestPermissions(this, new String[] { Manifest.Permission.SendSms }, REQUEST_SENDSMS);
}
}
//protected override void OnPause()
//{
// base.OnPause();
// UnregisterReceiver(receiver);
//}
protected override void OnResume()
{
//spinner.Visibility = (ViewStates.Gone);
base.OnResume();
//spinner.setVisibility(View.GONE);
_smsSentBroadcastReceiver = new SMSSentReceiver();
_smsDeliveredBroadcastReceiver = new SMSDeliveredReceiver();
RegisterReceiver(_smsSentBroadcastReceiver, new IntentFilter("SMS_SENT"));
RegisterReceiver(_smsDeliveredBroadcastReceiver, new IntentFilter("SMS_DELIVERED"));
//spinner.setVisibility(View.GONE);
}
protected override void OnPause()
{
base.OnPause();
UnregisterReceiver(_smsSentBroadcastReceiver);
UnregisterReceiver(_smsDeliveredBroadcastReceiver);
}
//[BroadcastReceiver(Exported = true, Permission = "//receiver/@android:android.permission.SEND_SMS")]
[BroadcastReceiver]
public class SMSSentReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
switch ((int)ResultCode)
{
case (int)Result.Ok:
Toast.MakeText(Application.Context, "SMS has been sent", ToastLength.Short).Show();
progress.Hide();
break;
case (int)SmsResultError.GenericFailure:
Toast.MakeText(Application.Context, "Generic Failure", ToastLength.Short).Show();
progress.Hide();
break;
case (int)SmsResultError.NoService:
Toast.MakeText(Application.Context, "No Service", ToastLength.Short).Show();
progress.Hide();
break;
case (int)SmsResultError.NullPdu:
Toast.MakeText(Application.Context, "Null PDU", ToastLength.Short).Show();
progress.Hide();
break;
case (int)SmsResultError.RadioOff:
Toast.MakeText(Application.Context, "Radio Off", ToastLength.Short).Show();
progress.Hide();
break;
default:
break;
}
}
}
//[BroadcastReceiver(Exported = true, Permission = "//receiver/@android:android.permission.SEND_SMS")]
[BroadcastReceiver]
public class SMSDeliveredReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
switch ((int)ResultCode)
{
case (int)Result.Ok:
Toast.MakeText(Application.Context, "SMS Delivered", ToastLength.Short).Show();
progress.Hide();
break;
case (int)Result.Canceled:
Toast.MakeText(Application.Context, "SMS not delivered", ToastLength.Short).Show();
progress.Hide();
break;
}
}
}
}
}
使用Android.App;
使用Android.OS;
使用Android.Support.V7.App;
使用Android.Runtime;
使用Android.Widget;
使用Android.Content.PM;
使用Android.Telephony;
使用制度;
使用安卓系统;
使用Android.Content;
使用Android.Support.V4.Content;
使用Android.Support.V4.App;
使用Android.Util;
使用Android.Support.Design.Widget;
使用Android.Views;
使用Android.Text;
使用系统线程;
名称空间MhylesOrderingApp
{
[活动(Label=“@string/app_name”,Theme=“@style/AppTheme”,MainLauncher=true)]
公共类MainActivity:AppCompativeActivity、ActivityCompat.IOnRequestPermissionsResultCallback
{
静态只读int请求\u SENDSMS=0;
私有SmsManager_SmsManager;
专用广播接收器_smsSentBroadcastReceiver,_smsDeliveredBroadcastReceiver;
视图布局;
//私人旋转棒;
静态Android.App.ProgressDialog进程;
//Android.App.ProgressDialog.progress;
//公共虚拟视图状态可见性{get;set;}
//SMS中央接收机;
//私有SmsManager SmsManager;
创建时受保护的覆盖无效(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
//从“主”布局资源设置视图
SetContentView(Resource.Layout.activity_main);
布局=FindViewById(Resource.Id.sample\u main\u布局);
//微调器=(ProgressBar)FindViewById(Resource.Id.progressBar1);
var smsBtn=findviewbyd(Resource.Id.btnSend);
var phoneNum=findviewbyd(Resource.Id.phoneNum);
var smsDate=“”;
var smsCustomer=findviewbyd(Resource.Id.txtCustomer);
var smsAddress=findviewbyd(Resource.Id.txtAddress);
var smsCustNo=findviewbyd(Resource.Id.txtCustomerNo);
var smsOrderProd=FindViewById(Resource.Id.txtOrderedProduct);
var smsProdQty=findviewbyd(Resource.Id.txtProductQty);
var smsUnit=findviewbyd(Resource.Id.txtnit);
var smsAgentName=findviewbyd(Resource.Id.txtAgentName);
var scrollView=findviewbyd(Resource.Id.scrollView);
DateTime now=DateTime.now.ToLocalTime();
smsDate=now.ToString();
scrollView.SetBackgroundResource(Resource.Drawable.TextViewStyle);
SetBackgroundResource(Resource.Drawable.EditTextStyle);
smsCustomer.SetBackgroundResource(Resource.Drawable.EditTextStyle);
smsAddress.SetBackgroundResource(Resource.Drawable.EditTextStyle);
smsCustNo.SetBackgroundResource(Resource.Drawable.EditTextStyle);
smsOrderProd.SetBackgroundResource(Resource.Drawable.EditTextStyle);
smsProdQty.SetBackgroundResource(Resource.Drawable.EditTextStyle);
SetBackgroundResource(Resource.Drawable.EditTextStyle);
smsAgentName.SetBackgroundResource(Resource.Drawable.EditTextStyle);
//spinner.Visibility=(ViewStates.Gone);
progress=newandroid.App.ProgressDialog(此对话框);
进程。不确定=真;
progress.SetProgressStyle(Android.App.ProgressDialogStyle.Spinner);
progress.SetMessage(“正在发送..请稍候!”);
进度。可设置可取消(false);
_smsManager=smsmsmanager.Default;
//接收器=新的SMSSentReceiver();
//smsManager=smsmsmanager.Default;
//IntentFilter IntentFilter=新的IntentFilter(“SMS_发送”);
//intentFilter.AddAction(“android.permission.SEND_SMS”);
////注册接收者(接收者、意图过滤器);
//smsBtn.单击+=(s,e)=>
//{
//var phone=phoneNum.Text;
//var message=sms.Text;
//var piSent=pendingent.GetBroadcast(this,0,新的Android.Content.Intent(“SMS_SENT”),0);
////var piDelivered=pendingent.GetBroadcast(this,0,新的Android.Content.Intent(“SMS_交付”),0);
//smsManager.SendTextMessage(phone,null,message,piSent,null);
//};
smsBtn.单击+=(s,e)=>
{
progress.Show();
if(TextUtils.IsEmpty(smsDate)| TextUtils.IsEmpty(phoneNum.Text)| TextUtils.IsEmpty(smsCustomer.Text)
||TextUtils.IsEmpty(smsAddress.Text)| | TextUtils.IsEmpty(smsCustNo.Text)| | TextUtils.IsEmpty(smsOrderProd.Text)
||TextUtils.IsEmpty(smsProdQty.Text)| | TextUtils.IsEmpty(smsUnit.Text)| | TextUtils.IsEmpty(smsAgentName.Text))
{
新线程(新线程开始(代理
{
RunOnUiThread(()=>Toast.MakeText(这是“请填写所有字段”,ToastLength.Long).Show());
RunOnUiThread(()=>progress.Hide());
})).Start();
//Toast.MakeText(这是“请填写所有字段”,ToastLength.Short).Show();
返回;
}
其他的
{
var phone=phoneNum.Text;
var message=smsDate+“|”+smsCustomer.Text+“|”+smsAddress.Text+“|”+smsCustNo.Text
+“|”+smsOrderProd.Text+“|”+smsProdQty.Text+“|”+smsUnit.Text+“|”+smsAgentName.Text;
editText.Text = "";