[英]onClickListener not working in my activity
My layout has 13 TextViews which on click changes the ListView Items.我的布局有 13 个 TextViews,单击它会更改 ListView 项目。
Here is my activity:这是我的活动:
public class ExampleActivity extends ListActivity implements
OnClickListener {
private String[] sa = new String[100];
private ListView lv;
private Context context = this;
private ArrayAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new LongOperation().execute("1");
lv = getListView();
}
private class LongOperation extends AsyncTask<String, Void, String> {
private ProgressDialog dialog = new ProgressDialog(
ExampleActivity.this);
@Override
protected String doInBackground(String... params) {
int i = Integer.parseInt(params[0]);
for (int n = 0; n < 100; n++) {
if (i != 5 && i != 10) {
sa[n] = "Item" + i;
} else {
}
}
return params[0];
}
@Override
protected void onPostExecute(String result) {
adapter = new ArrayAdapter<Object>(context,
android.R.layout.simple_list_item_1, sa);
lv.setAdapter(adapter);
this.dialog.dismiss();
}
@Override
protected void onPreExecute() {
this.dialog.setMessage("Please wait");
this.dialog.show();
}
@Override
protected void onProgressUpdate(Void... values) {
}
}
public void onClick(View v) {
Log.d("onClick", v.getId() + "**");
int id = v.getId();
switch (id) {
case R.id.tv1: {
new LongOperation().execute("1");
}
case R.id.tv2: {
new LongOperation().execute("2");
}
case R.id.tv3: {
new LongOperation().execute("3");
}
case R.id.tv4: {
new LongOperation().execute("4");
}
case R.id.tv5: {
new LongOperation().execute("5");
}
case R.id.tv6: {
new LongOperation().execute("6");
}
case R.id.tv7: {
new LongOperation().execute("7");
}
case R.id.tv8: {
new LongOperation().execute("8");
}
case R.id.tv9: {
new LongOperation().execute("9");
}
case R.id.tv10: {
new LongOperation().execute("10");
}
case R.id.tv11: {
new LongOperation().execute("11");
}
case R.id.tv12: {
new LongOperation().execute("12");
}
case R.id.tv13: {
new LongOperation().execute("13");
}
}
}
}
the listView is populated as item1 when i launch the app.当我启动应用程序时,listView 被填充为 item1。 but when i click on any of the TextViews, the onClick method is not triggered.
但是当我单击任何 TextViews 时,不会触发 onClick 方法。 i checked it using a Log.
我使用日志检查了它。
Thank You.谢谢。
Because you are not registering onClickListener
with your TextViews
hence your TextViews are not getting Clicked event.因为您没有使用您的
TextViews
注册onClickListener
因此您的 TextViews 没有获得 Clicked 事件。
For this you have to do something like,为此,您必须执行以下操作,
onCreate()
{
TextView tv1 = (TextVIew)findViewById(R.id.tv1);
tv1.setOnClickListener(this);
Better Solution:更好的解决方案:
In your Activity's xml Layout File,在您的 Activity 的 xml 布局文件中,
in your all TextView
put attribute android:onClick="textClick"
在你所有的
TextView
放置属性android:onClick="textClick"
Now remove onClickListener from your Activity and just write现在从您的活动中删除 onClickListener 并编写
public void textClick(View TextView)
in your activity.在你的活动中。 Then you don't have to register onClicklistener for all TextView.
那么你就不必为所有的 TextView 注册 onClicklistener。 Android does itself for you..
安卓为你做自己..
This is a sample program provided when you use implements OnClickListener这是您使用实现 OnClickListener 时提供的示例程序
public class ExampleActivity extends Activity implements OnClickListener {
protected void onCreate(Bundle savedValues) {
Button button = (Button)findViewById(R.id.corky);
button.setOnClickListener(this); // have a look on this line. registering.
}
// Implement the OnClickListener callback
public void onClick(View v) {
// do something when the button is clicked
}
}
发生这种情况是因为您没有为TextViews
使用setOnClickListener()
Add this static function in your activity class, This work for me in my MainActivity.java在您的活动类中添加这个静态函数,这在我的MainActivity.java中对我有用
public class MainActivity extends AppCompatActivity {
static {
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.