我正在尝试在 Android 上实现此代码,我是菜鸟

I'm trying to implement this code on Android, im a noob at this

我正在尝试制作一个学校应用程序,我想在每次按下按钮时制作一个弹出框。

但是我得到了错误:

expected class or package

来自这行代码:

showPopUp.OnClickListener

第 25 行

public class MainActivity extends ActionBarActivity {
@Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button showPopUp = (Button) findViewById(R.id.button);
    showPopUp.OnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            showSimplePopUp();
        }
    });
}

private void showSimplePopUp() {

    AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
    helpBuilder.setTitle("Opening Line");
    helpBuilder.setMessage("You popped my jingles up");
    helpBuilder.setPositiveButton("Ok",
            new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    // Do nothing but close the dialog
                }
            });

    // Remember, create doesn't show the dialog
    AlertDialog helpDialog = helpBuilder.create();
    helpDialog.show();
}




@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

看起来像打字错误,但应该是:

Button showPopUp = (Button) findViewById(R.id.button);
showPopUp.setOnClickListener(new View.OnClickListener(){

    @Override
    public void onClick(View v) {
        showSimplePopUp();
    }
});

你可以试试这个:

private AlertDialog showSimplePopUp() {

    AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
    helpBuilder.setTitle("Opening Line");
    helpBuilder.setMessage("You popped my jingles up");
    helpBuilder.setPositiveButton("Ok",
            new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    // Do nothing but close the dialog
                    dialog.dismiss();
                }
            });

    // Remember, create doesn't show the dialog
    helpBuilder.show();
    return helpBuilder.create();
}

希望对您有所帮助!!!