我使用Toast来显示一些消息但是有些麻烦。有我的代码:
private Button btPush;
private EditText etxMessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btPush = (Button) findViewById(R.id.btPushMe);
btPush.setOnClickListener(sayHello);
etxMessage = (EditText) findViewById(R.id.editText1);
}
View.OnClickListener sayHello = new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
LayoutInflater inflated = getLayoutInflater();
View layout = inflated.inflate(R.layout.activity_main,(ViewGroup) findViewById(R.id.layout));
String string = etxMessage.getText().toString();
Toast toast = new Toast(getApplicationContext());
toast.setText(string);
toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
//Toast.makeText(getApplicationContext(),string,Toast.LENGTH_SHORT).show();
}
};
如果我使用:
Toast.makeText(getApplicationContext(),string,Toast.LENGTH_SHORT).show();
有效!
答案 0 :(得分:1)
Toast.makeText(YourActivity.this,“你的文字在这里”,Toast.LENGTH_LONG).show();
答案 1 :(得分:0)
你可以尝试这个..如果错误仍然存在然后粘贴你的log-cat输出,我们很容易给你回答
Toast msg = Toast.makeText(ToastExample.this,"YOUR TEXT HERE", Toast.LENGTH_LONG);
msg.show();
答案 2 :(得分:0)
试试这个:
LayoutInflater inflated = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflated.inflate(R.layout.activity_main, null);
答案 3 :(得分:0)
试试这样的布局:
LayoutInflater inflated = LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflated.inflate(R.layout.activity_main, null);
答案 4 :(得分:0)
使用Toast显示消息的许多方法......
使用Toast对象...
Toast toast = new Toast(YourClassName.this);
或 尝试从click事件中显示然后使用此代码... 但这个'v'是View的对象。
Toast toast = new Toast(v.getContext());
或
以下代码,您可以在YourActivityClass内的任何地方使用...
Toast.makeText(YourClassName.this,“Your Message”,Toast.LENGTH_LONG).show();