3

このプログラムがトースト メッセージを表示しない理由を知っている人はいますか?

public class Main extends Activity implements OnClickListener{
/** Called when the activity is first created. */


  ImageButton x = (ImageButton) findViewById(R.id.imageButton1);
  ImageButton i = (ImageButton) findViewById(R.id.imageButton2);
  ImageButton question = (ImageButton) findViewById(R.id.imageButton3);

いくつかの ImageButtons とその他の要素を作成し、onClick 関数を作成しました

public void onClick(View v) {
            if(v.getId() == R.id.button1) // this works 
            {
                Intent intent = new Intent(this, Second.class);
                intent.putExtra("thetext", et1.getText().toString());
                intent.putExtra("thesize", et2.getText().toString());
                startActivity(intent);
            }
            if(v.getId() == R.id.imageButton2) // this wont work 
            {
                Toast toastI = Toast.makeText(this, "Testing", 5000);
                toastI.setGravity(Gravity.CENTER, 0, 0);
                toastI.show();
            }

ImageButton i (プログラムを実行した後) をクリックすると、トーストが表示されませんか?

4

5 に答える 5

1

imagebuttons に onclickListener を設定したことを願っています。

  i.setOnClickListener(this);

これを試して

Toast.makeText(getApplication(), "Testing", 5000).show();
于 2012-06-23T16:40:01.900 に答える
0

あなたの問題は、トーストの長さに5000を入れていると思います。LENGTH_LONGLENGTH_SHORT の値は 1 と 0 であるため、フラグとして使用されます。したがって、5000 を入力するとどうなるかわかりません (おそらく何もない)。また、Javaドキュメントでは、または他のものを配置する必要があると言われています。したがって、どちらかを使用してください。

public static Toast makeText (Context context, CharSequence text, int duration) 導入されたバージョン: API レベル 1 テキスト ビューだけを含む標準のトーストを作成します。

パラメータ context 使用するコンテキスト。通常は Application または Activity オブジェクトです。text 表示するテキスト。書式設定されたテキストにすることができます。duration メッセージを表示する期間。LENGTH_SHORT または LENGTH_LONG のいずれか

于 2012-06-23T17:28:23.707 に答える
0

static Toast makeText(Context context, int resId, int duration)

5000問題は、メソッドに 3 番目のパラメーターとして渡していることです。ユーザーに表示される秒数 (またはミリ秒) でint durationはありませんToast。このToastクラスでは、可能な値を 2 つだけ渡す必要があります (これは理にかなっています。そうしないと、開発者がトースト メッセージの長さを完全に乱用し、Android マーケットのアプリケーションがユーザーにメッセージを表示する方法に完全に一貫性がなくなるからです)。2 つの値は次のとおりです。

Toast.LENGTH_SHORT 
Constant Value: 0 (0x00000000) 

また

Toast.LENGTH_LONG`
Constant Value: 1 (0x00000001)

この問題を解決するには、メソッド呼び出しを次のように変更します。

Toast.makeText(this, "Testing", Toast.LENGTH_LONG).show();

onClickListenerまた、あなたの sに設定することを忘れないでくださいImageButton(暗闇の中でのショットだけです)。

于 2012-06-23T18:00:22.950 に答える
0

switchas の代わりに case を使用し、 asの代わりにorifを使用してみてください。Main.thisgetApplicationContext()this

public void onClick(View v) {

switch (v.getId()) {
    case R.id.button1:
    Intent intent = new Intent(Main, Second.class);
            intent.putExtra("thetext", et1.getText().toString());
            intent.putExtra("thesize", et2.getText().toString());
            startActivity(intent);
    break;
    case R.id.imageButton3:
        Toast toastI = Toast.makeText(Main.this, "Testing", 5000);
        toastI.setGravity(Gravity.CENTER, 0, 0);
        toastI.show();
    break;
 }
}
于 2012-06-23T17:00:18.650 に答える