简体   繁体   English

按下按钮无法打开聊天意图

[英]I can't open a chat Intent by pressing a button

I can't open a new Intent by a button but only in listview I followed a guide but it solved it listview and I need a normal click where is the problem in my code 我无法通过按钮打开新的Intent,但只能在listview中遵循指南,但它解决了listview,因此我需要正常单击才能在代码中找到问题所在

The code only works with the listview... 该代码仅适用于列表视图...


 buttonadd.setOnClickListener(new AdapterView.OnClickListener(){
                   @Override
                   public void onClick(AdapterView<?> parent, View view, int postition, long id){



                       String curentgruopname = parent.getItemAtPosition(postition).toString();
                       Intent groupchatintent = new Intent(MainActivity.this , buttonaddactivity.class);
                       groupchatintent.putExtra("groupname", curentgruopname);

                       startActivity(groupchatintent);



                   }
                        });


 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int postition, long id) {

                String curentgruopname = parent.getItemAtPosition(postition).toString();
                Intent groupchatintent = new Intent(MainActivity.this , buttonaddactivity.class);
                groupchatintent.putExtra("groupname", curentgruopname);

                startActivity(groupchatintent);


            }
        });

Maybe it works when you initialize the OnClicklistener in the OnCreate Method. 当您在OnCreate方法中初始化OnClicklistener时,它可能起作用。

Here is an Example: 这是一个例子:

Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_test);
    button = findViewById(R.id.button);
    button.setOnClickListener(buttonClick);
}

View.OnClickListener buttonClick = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(testActivity.this, nextActivity.class);
        startActivity(intent);
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM