activity_verify.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:background="#ffffff"
android:gravity="center_vertical">
<TextView
android:id="@+id/countdown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:focusable="false"
android:fontFamily="sans-serif"
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
android:textColor="@android:color/black"
android:textSize="30sp"
android:textStyle="bold" />
<EditText
android:id="@+id/otp"
style="@android:style/Widget.DeviceDefault.Light.EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:maxLength="4"
android:layout_below="@id/countdown"
android:layout_marginTop="10dp"
android:layout_marginBottom="30dp"
android:hint="Enter 4 digit OTP sent to your phone"/>
<Button
android:id="@+id/verification"
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginBottom="60dp"
android:background="@android:color/holo_blue_dark"
android:fontFamily="sans-serif"
android:text="@string/bttext"
android:textAlignment="center"
android:textColor="@android:color/white"
android:textSize="18sp"
android:textStyle="bold"
android:layout_below="@id/otp"/>
</RelativeLayout>
Verify.java
package com.example.myapp.appfirst;
import android.app.Activity;
import android.content.Context;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.view.View;
import android.widget.EditText;
.
.
.
public class Verify extends Activity{
public Button bt;
public EditText digits;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_verify);
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);
bt = (Button) findViewById(R.id.verification);
digits = (EditText) findViewById(R.id.otp);
getdata(); //This function sends otp to the phone and sets countdown timer for OTP verification
Log.d("oncreate","Now going to buttonVerify()");
buttonVerify();
}
public void buttonVerify() {
Log.d("Button","Entered into buttonVerify");
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("Button clicked","yaah!!");
Toast.makeText(getBaseContext(),"It's working",Toast.LENGTH_SHORT).show();
.
.
.
}
});
}
.
.
.
Logcat消息&#34;按钮点击/ yaah !!&#34;从未显示过,并且onClick方法中的toast消息永远不会显示。该按钮完全没有点击。 我已经浏览了stackoverflow上的所有代码和问题,我不知道我的代码有什么问题,而在我之前创建的其他应用程序中,相同的代码运行得很好。
答案 0 :(得分:1)
可以this.requestWindowFeature(Window.FEATURE_NO_TITLE);
上面的setContentView(R.layout.test);
您的活动
public class Verify extends Activity {
public Button bt;
public EditText digits;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_verify);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); //App with fullscreen
bt = (Button) findViewById(R.id.verification);
digits = (EditText) findViewById(R.id.otp);
//getdata(); //This function sends otp to the phone and sets countdown timer for OTP verification
Log.d("oncreate", "Now going to buttonVerify()");
buttonVerify();
}
public void buttonVerify() {
Log.d("Button", "Entered into buttonVerify");
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("Button clicked", "yaah!!");
Toast.makeText(getBaseContext(), "It's working", Toast.LENGTH_SHORT).show();
}
});
}
}
请参阅:布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:background="#ffffff"
android:gravity="center_vertical">
<TextView
android:id="@+id/countdown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:focusable="false"
android:fontFamily="sans-serif"
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
android:textColor="@android:color/black"
android:textSize="30sp"
android:textStyle="bold" />
<EditText
android:id="@+id/otp"
style="@android:style/Widget.DeviceDefault.Light.EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:maxLength="4"
android:layout_below="@id/countdown"
android:layout_marginTop="10dp"
android:layout_marginBottom="30dp"
android:hint="Enter 4 digit OTP sent to your phone"/>
<Button
android:id="@+id/verification"
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginBottom="60dp"
android:background="@android:color/holo_blue_dark"
android:fontFamily="sans-serif"
android:text="bttext"
android:textAlignment="center"
android:textColor="@android:color/white"
android:textSize="18sp"
android:textStyle="bold"
android:layout_below="@id/otp"/>
</RelativeLayout>
答案 1 :(得分:0)
删除buttonVerify()方法...... 使用这个....
public class Verify extends Activity{
public Button bt;
public EditText digits;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_verify);
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);
bt = (Button) findViewById(R.id.verification);
digits = (EditText) findViewById(R.id.otp);
getdata(); //This function sends otp to the phone and sets countdown timer for OTP verification
Log.d("oncreate","Now going to buttonVerify()");
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("Button clicked","yaah!!");
Toast.makeText(getBaseContext(),"It's working",Toast.LENGTH_SHORT).show();
}
});
}