Verify.java
public class Verify extends Activity{
public Button bt;
public EditText digits;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); //App with fullscreen
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_verify);
bt = (Button) findViewById(R.id.verification);
digits = (EditText) findViewById(R.id.otp);
getdata();
buttonVerify();
}
public void buttonVerify() {
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getBaseContext(),"It's working",Toast.LENGTH_SHORT).show();
}
});
}
private void getdata() {
//This function uses retrofit for receiving user information including name and phone number from the server (PHP and mysql) and sets it into the TextView and calls to sendOTP() method here->
sendOTP();
}
public void sendOTP(){
//this method now again sends the phone number to another php file on server which uses the php free sms api and sends the message on the user's number. Everything works fine and gets sms in 5-7 seconds.
}
}
在onCreate()
方法中,每当我移除getdata()
函数调用时,Button
bt点击都会很好并显示"它正在工作"。但是,只要我从getdata()
调用onCreate()
功能,该按钮就会显示在布局中,但不会点击。而短信到达手机的背景。有什么想法和帮助吗?
答案 0 :(得分:0)
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;
using System.Collections;
namespace ContosoUniversityModelBinding.Models
{
public class SchoolContext : DbContext
{
public DbSet<Student> Students { get; set; }
public DbSet<Enrollment> Enrollments { get; set; }
public DbSet<Course> Courses { get; set; }
}
public class Student
{
[Key, Display(Name = "ID")]
[ScaffoldColumn(false)]
public int StudentID { get; set; }
[Required, StringLength(40), Display(Name = "Last Name")]
public string LastName { get; set; }
[Required, StringLength(20), Display(Name = "First Name")]
public string FirstName { get; set; }
[EnumDataType(typeof(AcademicYear)), Display(Name = "Academic AcademicYear")]
public AcademicYear Year { get; set; }
[Range(typeof(DateTime), "1/1/2013", "1/1/3000", ErrorMessage = "Please provide an enrollment date after 1/1/2013")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
public DateTime EnrollmentDate { get; set; }
[Range(typeof(DateTime), "1/1/1900", "1/1/3000", ErrorMessage = "Please provide a Deadline for this project!")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
public DateTime checkTime { get; set; }//each added student has a timer
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (DateTime.Now >= checkTime)
{
yield return
new ValidationResult(errorMessage: "Project Deadline Reached/Surpassed",
memberNames: new[] { "checkTime" });
}
}
public virtual ICollection<Enrollment> Enrollments { get; set; }
}
public class Enrollment
{
[Key]
public int EnrollmentID { get; set; }
public int CourseID { get; set; }
public int StudentID { get; set; }
public decimal? Grade { get; set; }
public virtual Course Course { get; set; }
public virtual Student Student { get; set; }
}
public class Course
{
[Key]
public int CourseID { get; set; }
public string Title { get; set; }
public int Credits { get; set; }
public virtual ICollection<Enrollment> Enrollments { get; set; }
}
public enum AcademicYear
{
Freshman,
Sophomore,
Junior,
Senior
}
}
完成请求后,您可以致电protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); //App with fullscreen
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_verify);
bt = (Button) findViewById(R.id.verification);
digits = (EditText) findViewById(R.id.otp);
bt.setEnabled(false);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getBaseContext(),"It's working",Toast.LENGTH_SHORT).show();
}
});
getdata();
}
。
据我所知,在Retrofit中,您可以在MainThread中的bt.setEnabled(true);
方法的末尾写下这样的内容:
onResponse()
答案 1 :(得分:0)
您需要在UI线程以外的单独线程中执行API调用。因此,创建一个AsyncTask
并在那里执行该方法。
因此,根据您的问题,我猜您从API调用中获取了一些数据,并且在该调用成功后,您将通过另一个API调用发送OTP。因此,为了顺利运行这些服务调用,您可以考虑以下实现。
创建一个界面并将其用作服务调用的监听器。
public interface HttpResponseListener {
void httpResponseReceiver(String result);
}
现在创建一个AsyncTask
,用于从服务器获取数据。
public class HttpRequestAsyncTask extends AsyncTask<Void, Void, String> {
public HttpResponseListener mHttpResponseListener;
private final Context mContext;
HttpRequestAsyncTask(Context context, HttpResponseListener listener) {
this.mContext = context;
this.mHttpResponseListener = listener;
}
@Override
protected String doInBackground(Void... params) {
String result = getData();
}
@Override
protected void onPostExecute(final String result) {
// Check if the API call is a success and send the result back to your calling Activity.
mHttpResponseListener.httpResponseReceiver(result);
}
@Override
protected void onCancelled() {
mHttpResponseListener.httpResponseReceiver(null);
}
}
创建另一个AsyncTask
,用于通过其他服务呼叫发送OTP。
public class SendOTPAsyncTask extends AsyncTask<Void, Void, Void> {
private final Context mContext;
SendOTPAsyncTask(Context context) {
this.mContext = context;
}
@Override
protected void doInBackground(Void... params) {
String result = sendOTP();
}
@Override
protected void onPostExecute() {}
@Override
protected void onCancelled() {}
}
现在从Activity
设计中,服务调用就像这样流动。
public class YourActivity extends Activity implements HttpResponseListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ... Other code
new HttpRequestAsyncTask(this, this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
buttonVerify();
}
@Override
public void httpResponseReceiver(String result) {
// Check if success. If success, send the OTP.
new SendOTPAsyncTask(this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}