setOnClickListener方法

setOnClickListener方法

下面是示例代码,我正在尝试从setOnClickListener方法的Spinner中为按钮validateUser获取选定的值。

    public class MainActivity extends Activity implements  AdapterView.OnItemSelectedListener {
      private Button validateUser;
      private Spinner userTypeSpinner;


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

        userTypeSpinner=(Spinner)findViewById(R.id.userTypeSpinner);
        validateUser= (Button) findViewById(R.id.userValidate);

        validateUser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getApplicationContext(), "Button is Selected", Toast.LENGTH_SHORT);
            }
        });



        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.userTypeArray, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        userTypeSpinner.setAdapter(adapter);
        userTypeSpinner.setOnItemSelectedListener(this);
    }

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        String item = adapterView.getItemAtPosition(i).toString();

        Toast.makeText(adapterView.getContext(), "Selected: " + item, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {

    }
}

最佳答案

烤面包需要显示。最后使用.show()

Toast.makeText(getApplicationContext(), "Button is Selected", Toast.LENGTH_SHORT).show();

关于java - 不调用按钮setOnClickListener方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30799360/

10-10 19:30