【问题标题】:multiple intents on different items of CustomListViewCustomListView 不同项目的多个意图
【发布时间】:2018-09-28 23:08:39
【问题描述】:

每当用户点击“电话图标”和短信图标时,就会有意向拨打号码或发送短信。我不知道如何编写我的 OnClickListener 以便列表视图中的两个图标响应点击事件

MainActivity.java

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

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

        final ArrayList<Student> students = new ArrayList<Student>();

        ListView studentListView = (ListView) findViewById(R.id.list);

        StudentAdapter adapter = new StudentAdapter(this, R.layout.item_list, students);

        studentListView.setAdapter(adapter);

        studentListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

                Student student = students.get(position);
                String url = student.getStudentNumber();

                           Student student = students.get(position);
            String url = student.getStudentNumber();
            Intent smsIntent = new Intent(android.content.Intent.ACTION_VIEW);
            smsIntent.setType("vnd.android-dir/mms-sms");
            smsIntent.putExtra("address", url);
            smsIntent.putExtra("sms_body", "your desired message");
            startActivity(smsIntent);
                }
            }
        });
    }
}

当我单击 ListView 项目的任何位置时,此 Sms 意图工作正常,但是 假设每个项目中有一个带有“sms icon”和R.id.smsButton,我只希望它响应点击事件并启动意图,而不是我的 CustomListView 的项目中的任何其他视图。 `

【问题讨论】:

    标签: android listview android-intent onclicklistener


    【解决方案1】:

    在适配器类本身的 getView() 方法中编写点击监听器

    public View getView(int pos, View convertView, ViewGroup parent){
    ...
    Button button = (Button) row.findViewById(R.id.button);
    button.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
    
             //do something
    
        }
    });
    ...
    return row;
    

    }

    【讨论】:

      猜你喜欢
      • 2017-12-25
      • 2015-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-17
      • 1970-01-01
      • 2016-10-29
      相关资源
      最近更新 更多