当前位置: 首页 > 知识库问答 >
问题:

android studio上toast的按钮单击事件错误

归鸿朗
2023-03-14

这段代码不在Android Studio上运行。我只想让它在点击按钮时显示一个祝酒词。请帮帮忙。代码附在下面:

我是android开发的新手,很难搞清楚这一点。我认为问题是一个愚蠢的语法错误或变量没有被定义。还有,我怎么定义“V”?

package com.shutter2000.www.shutter_2000_oms;    
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import android.view.View.OnClickListener;    


public class SiteVisitDetails extends Activity {

public EditText mCusName;
public EditText mOrderID;
public EditText mProjectID;
public EditText mHeightWidth;
public EditText mNote;
public Button mSubmitButton;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_site_visit_details);

    //initialize text fields
    mCusName = (EditText) findViewById(R.id.txtCusName);
    mOrderID = (EditText) findViewById(R.id.txtOrderID);
    mProjectID = (EditText) findViewById(R.id.txtProjectID);
    mHeightWidth = (EditText) findViewById(R.id.txtHeightWidth);
    mNote = (EditText) findViewById(R.id.txtNote);
    mSubmitButton = (Button) findViewById(R.id.btnSubmit);

    //listen to submit button click event

    mSubmitButton.OnClickListener(new View.OnClickListener())
    {

        @Override
        public void onClick(View v){


        if (v == mSubmitButton) {
            Context context = getApplicationContext();
            CharSequence text = "Successfully Sent!";
            int duration = Toast.LENGTH_LONG;
            final Toast tost = Toast.makeText(context, text, duration);
            tost.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.site_visit_details, menu);
    return true;
}

public void addItemsToProductSpinner() {
    Spinner spinner = (Spinner) findViewById(R.id.spnProducts);
    // Create an ArrayAdapter using the string array and a default spinner layout
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
            R.array.products, android.R.layout.simple_spinner_item);
    // Specify the layout to use when the list of choices appears
    android.R.layout.simple_spinner_item
    // Apply the adapter to the spinner
    spinner.setAdapter(adapter);
}

@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();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}    

错误有:

“MSubmitButton...”出错

Expected class or package
'OnClickListener' is abstract;cannot be instantiated

“@override public void onClick(View v){...”出错

Annotations are not allowed here
Identifier or type expected

“Public void addItemsToProductSpinner()...”错误

";" expected
Cannot resolve symbol "v"
unexpected token

共有1个答案

闾丘高峰
2023-03-14
package com.shutter2000.www.shutter_2000_oms;    
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import android.view.View.OnClickListener;    


public class SiteVisitDetails extends Activity implements OnClickListener{

public EditText mCusName;
public EditText mOrderID;
public EditText mProjectID;
public EditText mHeightWidth;
public EditText mNote;
public Button mSubmitButton;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_site_visit_details);

    //initialize text fields
    mCusName = (EditText) findViewById(R.id.txtCusName);
    mOrderID = (EditText) findViewById(R.id.txtOrderID);
    mProjectID = (EditText) findViewById(R.id.txtProjectID);
    mHeightWidth = (EditText) findViewById(R.id.txtHeightWidth);
    mNote = (EditText) findViewById(R.id.txtNote);
    mSubmitButton = (Button) findViewById(R.id.btnSubmit);

    //listen to submit button click event

    mSubmitButton.setOnClickListener(this);

}

@Override
public void onClick(View v) {
     if (v.getId() == R.id.mSubmitButton) {
         Toast.makeText(this,"Successfully sent!",Toast.LENGTH_LONG).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.site_visit_details, menu);
    return true;
}

public void addItemsToProductSpinner() {
    Spinner spinner = (Spinner) findViewById(R.id.spnProducts);
    // Create an ArrayAdapter using the string array and a default spinner layout
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
            R.array.products, android.R.layout.simple_spinner_item);
    // Specify the layout to use when the list of choices appears
    android.R.layout.simple_spinner_item
    // Apply the adapter to the spinner
    spinner.setAdapter(adapter);
}

@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();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}  
 类似资料:
  • 我需要创建一个按钮,如下所示: HTML 如何在JS文档中指定这些函数?

  • 问题内容: 似乎是一个简单的问题,但解决其他问题的方法似乎对我没有用。 试图通过单击按钮来触发AJAX请求,但似乎并未触发。 HTML示例 javascript 问题答案: 您拥有的代码在拨弄中工作得很好。初始页面加载后,按钮是否通过AJAX动态呈现? 用 代替

  • 问题内容: 我想知道是否有人可以指出我正确的方向。我有一个带有click事件的asp.net按钮(运行一些服务器端代码)。我想做的是通过ajax和jquery调用此事件。有什么办法吗?如果是这样,我会喜欢一些例子。 提前致谢 问题答案: 这就是jQuery对于ASP.Net开发人员真正的亮点。假设您有这个ASP按钮: 呈现后,您可以查看页面的源,页面上的ID不会是btnAwesome,而是$ ct

  • 问题内容: 这是我想做的事情:单击页面上的一个按钮,这又使(2)事情发生: 显示一个ModalPopup,以防止用户按下任何按钮或更改值 在方法后面调用我的代码,完成后隐藏ModalPopup 这是ASP标记: 现在,这是我在C#代码后面的代码: 为什么不起作用?ModalPopup可以完美显示,但是btnSaveData_Click事件从不触发。 更新: 第一个建议对我不起作用。我还尝试了您的第

  • 我想在单击JFrame标题栏的红色close按钮时调用一个方法。 我如何捕捉那个事件?

  • 我有一些PHP和HTML: 还有一些jQuery,它应该根据选择简单地调出一个带有no或yes的JavaScript窗口: 但是无论我点击是或否按钮,它都会返回是: 我做错了什么? 编辑: