在阅读了Android的Toast课程后,我不得不尝试一下 我在布局上添加了一个按钮,按照this页面上的说明操作,并在我的按钮上添加了一个OnClickListener,可以调用toast。 现在的问题是,当我调试应用程序时,该按钮不会出现在视图上。
我有什么不应该的东西吗? (可根据要求提供其他信息)
代码:
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Button" />
</RelativeLayout>
MainActivity.java
package com.joseph.toasttest;
import com.joseph.toasttest.R;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.app.Activity;
import android.content.Context;
public class MainActivity extends Activity {
Context context = getApplicationContext();
CharSequence text = "Test";
int duration = Toast.LENGTH_SHORT;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnToast = (Button) findViewById(R.id.button1);
btnToast.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
toastMe(v);
}
});
}
private void toastMe(View v) {
Toast.makeText(context, text, duration).show();
}
}
答案 0 :(得分:0)
试试这个:
btnToast.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast msg = Toast.makeText(MainActivity.this, text, duration );
msg.show();
答案 1 :(得分:0)
您不能将应用程序上下文用于UI内容,例如创建对话框,Toast或启动活动。只需将活动用作上下文。
Different contexts in Android, and what they can and can't be used for.
答案 2 :(得分:0)
只是猜测一下,但我会开始删除RelativeLayout
中的填充内容。您可能正在一个值为
@dimen/activity_vertical_margin
和/或
@dimen/activity_horizontal_margin
将“移动”按钮移出屏幕。您也可以尝试在视图中心对齐按钮.-
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Button" />
答案 3 :(得分:0)
您必须在onCreate()
方法中创建上下文:
package com.joseph.toasttest;
import com.joseph.toasttest.R;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.app.Activity;
import android.content.Context;
public class MainActivity extends Activity {
Context context = getApplicationContext();
CharSequence text = "Test";
int duration = Toast.LENGTH_SHORT;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnToast = (Button) findViewById(R.id.button1);
btnToast.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
toastMe(v);
}
});
}
private void toastMe(View v) {
Toast.makeText(context, text, duration).show();
}
}
答案 4 :(得分:0)
我通过完全删除上下文和持续时间变量并在我的Toast方法中手动设置它来实现它。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnToast = (Button) findViewById(R.id.button1);
btnToast.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
toastMe(v);
}
});
}
private void toastMe(View v) {
Toast.makeText(getApplicationContext(), "Test", Toast.LENGTH_SHORT).show();
}
}
答案 5 :(得分:0)
这应该有效:
package com.joseph.toasttest;
import com.joseph.toasttest.R;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.app.Activity;
import android.content.Context;
public class MainActivity extends Activity {
CharSequence text = "Test";
int duration = Toast.LENGTH_SHORT;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnToast = (Button) findViewById(R.id.button1);
btnToast.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
toastMe(this);
}
});
}
private void toastMe(Context context) {
Toast.makeText(context, text, duration).show();
}
}