Android TextView(在ListView中)单击文本本身时单击未注册的项
我制作了一个InfoscreenActivity,在我的应用程序中显示一个“设置/关于”页面(包含应用程序版本信息、许可证、联系人、设置和备份选项)。该页面上的项目是使用名为InfoscreenAdapter的自定义ArrayAdapter在ListView中生成的 下面是相关的类和xml布局文件 活动信息屏幕活动:Android TextView(在ListView中)单击文本本身时单击未注册的项,android,android-listview,onitemclick,Android,Android Listview,Onitemclick,我制作了一个InfoscreenActivity,在我的应用程序中显示一个“设置/关于”页面(包含应用程序版本信息、许可证、联系人、设置和备份选项)。该页面上的项目是使用名为InfoscreenAdapter的自定义ArrayAdapter在ListView中生成的 下面是相关的类和xml布局文件 活动信息屏幕活动: public class InfoscreenActivity extends ActionBarActivity { private static final Strin
public class InfoscreenActivity extends ActionBarActivity {
private static final String TAG = "InfoscreenActivity";
private InfoscreenItem appVersion;
private InfoscreenItem iiLicenses;
private InfoscreenItem contactUs = new InfoscreenItem("Contact us");
private InfoscreenItem iiSetup = new InfoscreenItem("Launch Setup");
private InfoscreenItem iiBackup = new InfoscreenItem("Perform Backup");
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
if(AppConfig.DEBUG) Log.i(TAG, "launching onCreate() ...");
String appVersionString;
if(AppConfig.DEBUG) Log.i(TAG, "trying to fetch the version name ...");
try {
appVersionString = SetupActivity.getVersionName(this);
appVersion = new InfoscreenItem("App version", appVersionString);
} catch (Exception e) {
if(AppConfig.DEBUG) Log.e(TAG, e.getMessage());
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_infoscreen);
iiLicenses = new InfoscreenItem(getResources().getString(R.string.title_activity_licenses));
// Get ListView object from xml
lv = (ListView) findViewById(R.id.infoscreenListViewID);
// Defined Array values to show in ListView
InfoscreenItem[] items = new InfoscreenItem[] {appVersion, contactUs, iiLicenses, iiSetup, iiBackup};
// Define a new InfoscreenAdapter
InfoscreenAdapter adapter = new InfoscreenAdapter(this, items);
// Assign adapter to ListView
if(AppConfig.DEBUG) Log.i(TAG, "setting adapter");
lv.setAdapter(adapter);
// React to user clicks on item
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parentAdapter, View view, int position,
long id) {
if(AppConfig.DEBUG) Log.i(TAG, "item " + Integer.toString(position) + " clicked.");
// for the "Contact us" item
if(position == 1) {
TextView clickedView = (TextView) view.findViewById(R.id.textView);
onContactClick(clickedView);
}
// for the "Licenses" item
if(position == 2) {
TextView clickedView = (TextView) view.findViewById(R.id.textView);
onLicensesClick(clickedView);
}
// for the "Setup" item
if (position == 3) {
if (AppConfig.DEBUG) Log.d(TAG, "launching Setup Activity...");
startActivity(new Intent(getApplication(), SetupActivity.class));
}
// for the "Backup" item
if (position == 4) {
Intent iBackup = new Intent(getApplication(), BackupAndRestoreActivity.class);
iBackup.putExtra("MODE", 1);
startActivity(iBackup);
}
}
});
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if(AppConfig.DEBUG) Log.i(TAG, "exiting onCreate() ...");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if(AppConfig.DEBUG) Log.i(TAG, "onCreateOptionsMenu() ...");
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_infoscreen, menu);
if(AppConfig.DEBUG) Log.i(TAG, "exiting onCreateOptionsMenu() ...");
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;
}
else if (id == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
public void onContactClick(View v) {
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[]{"contact@gmail.com"});
email.putExtra(Intent.EXTRA_SUBJECT, "(subject)");
email.putExtra(Intent.EXTRA_TEXT, "(message)");
email.setType("message/rfc822");
startActivity(Intent.createChooser(email, "Choose an Email client :"));
}
public void onLicensesClick(View v) {
if(AppConfig.DEBUG) Log.i(TAG, "onLicensesClick() ...");
startActivity(new Intent(InfoscreenActivity.this, LicensesActivity.class));
}
}
用于格式化的xml文件
activity_infoscreen.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="at.dimi3.crew2go.InfoscreenActivity"
tools:ignore="MergeRootFrame"
android:orientation="vertical">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/infoscreenListViewID"
android:clickable="true" />
</LinearLayout>
item_infoscreen_single_line.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textView"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:paddingRight="20dp"
android:paddingBottom="10dp"
android:clickable="true" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textView_title"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:paddingRight="20dp"
android:paddingBottom="5dp"
android:clickable="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="@+id/textView_versionName"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingBottom="10dp"
android:clickable="true" />
</LinearLayout>
item_infoscreen_dual_line.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textView"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:paddingRight="20dp"
android:paddingBottom="10dp"
android:clickable="true" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textView_title"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:paddingRight="20dp"
android:paddingBottom="5dp"
android:clickable="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="@+id/textView_versionName"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingBottom="10dp"
android:clickable="true" />
</LinearLayout>
现在,除了在“信息屏幕”的列表视图中,项目只能单击文本旁边的项目外,所有项目都按预期工作。是否有任何方法可以使整个ListView项目对单击作出反应,而不管您在何处单击该项目?xml布局模板中是否缺少某些内容?我尝试过设置android:clickable=“true”,删除它,尝试改变android:layout\u width=“wrap\u content”以匹配父对象或填充父对象(正如其他类似线程中所建议的那样,这些都是不可点击的,所以问题与我现在遇到的完全不同)在您的
item\u infoscreen\u dual\u line.xml或item\u infoscreen\u single\u line.xml
中(我不知道是哪一个引起了您的麻烦)为您的线性布局提供一个id。现在找到该布局并在该布局上设置OnClickListener。简单。您已为textview添加了填充。但您希望单击整个LinearLayout项目。因此,不要将填充内容写入TextView,而是将其用于LinearLayout好的,非常感谢您的输入。我在谷歌上做了更多的搜索,无意中发现了这个项目,并在我的Android Studio中创建了这个项目。在本例中,单击在任何地方都有效,因此我开始逐个删除xml属性以匹配示例中的xml,结果发现删除android:clickable=“true”解决了这个问题。我有点困惑(听起来有点矛盾),但它是有效的。你的意思是在线性布局中不使用文本视图?(顺便说一句,两者都有相同的问题…)。因为现在,我将onClickListener设置为ListView本身,并在onItemClick()中获取TextView。