Listactivity文字和按钮单击奇怪的行为 [英] Listactivity With Text and Button Click Strange Behavior

查看:149
本文介绍了Listactivity文字和按钮单击奇怪的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个listactivity应用程序,每行cotain TEXT和按键,文本和按钮都必须可以点击(什么IM设法得到它),当单击该文本将打开MyDay活动,当点击按钮,它会打开的my_videos活动

当你打开应用程序并点击任何按钮,什么确切地发生,它做的没有点击的动作在所有行任何按钮,但是当你点击任何一行文本,它会打开MyDay活动然后单击第一行仅下,它将打开的my_videos活性是videoview,但在同一时间的其它按钮不处于行的其余部分可点击

任何意见将AP preciated,

感谢。

我的code:

MyArrayAdapter类:

 公共类MyArrayAdapter扩展ArrayAdapter<串GT; {
私人最终活动范围内;
私人最终的String []班;
按钮BT1,BT2,BT3,BT4,BT5;字体TF;公共MyArrayAdapter(Activity上下文,字符串[]班){
    超(背景下,R.layout.row,班);
    this.context =背景;
    this.classes =班;
              }@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
    TF = Typeface.createFromAsset(context.getAssets(),BFantezy.ttf);
    LayoutInflater吹气= context.getLayoutInflater();
    查看rowView = inflater.inflate(R.layout.row,空,真);
    TextView中的TextView =(TextView中)rowView.findViewById(R.id.row_label);
    按钮BT1 =(按钮)rowView.findViewById(R.id.button1);
    按钮BT2 =(按钮)rowView.findViewById(R.id.button2);
    按钮BT3 =(按钮)rowView.findViewById(R.id.button3);
    按钮BT4 =(按钮)rowView.findViewById(R.id.button4);
    按钮BT5 =(按钮)rowView.findViewById(R.id.button5);    字符串s =类[位置]
    textView.setText(多个);
    ((的TextView)的TextView).setTypeface(TF);
如果(s.startsWith(第一)){    bt1.setBackgroundResource(R.drawable.ic_launcher);
    bt2.setBackgroundResource(R.drawable.ic_launcher);
    bt3.setBackgroundResource(R.drawable.ic_launcher);
    bt4.setBackgroundResource(R.drawable.ic_launcher);
    bt5.setBackgroundResource(R.drawable.ic_launcher); }如果(s.startsWith(二)){    bt1.setBackgroundResource(R.drawable.ic_launcher);
    bt2.setBackgroundResource(R.drawable.ic_launcher);
    bt3.setBackgroundResource(R.drawable.ic_launcher);
    bt4.setBackgroundResource(R.drawable.ic_launcher);
    bt5.setBackgroundResource(R.drawable.ic_launcher); }如果(s.startsWith(第三)){    bt1.setBackgroundResource(R.drawable.ic_launcher);
    bt2.setBackgroundResource(R.drawable.ic_launcher);
    bt3.setBackgroundResource(R.drawable.ic_launcher);
    bt4.setBackgroundResource(R.drawable.ic_launcher);
    bt5.setBackgroundResource(R.drawable.ic_launcher); }如果(s.startsWith(四)){    bt1.setBackgroundResource(R.drawable.ic_launcher);
    bt2.setBackgroundResource(R.drawable.ic_launcher);
    bt3.setBackgroundResource(R.drawable.ic_launcher);
    bt4.setBackgroundResource(R.drawable.ic_launcher);
    bt5.setBackgroundResource(R.drawable.ic_launcher); }如果(s.startsWith(十五)){    bt1.setBackgroundResource(R.drawable.ic_launcher);
    bt2.setBackgroundResource(R.drawable.ic_launcher);
    bt3.setBackgroundResource(R.drawable.ic_launcher);
    bt4.setBackgroundResource(R.drawable.ic_launcher);
    bt5.setBackgroundResource(R.drawable.ic_launcher); }返回rowView; }}

的my_videos类:

 公共类的my_videos延伸活动{
私人VideoView VID;
夜弦;
/ **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.video);    集束BDL = getIntent()getExtras()。
    晚上= bdl.getString(视频);    VID =(VideoView)findViewById(R.id.videoView1);      如果(night.equalsIgnoreCase(按钮1)){
         vid.setVideoURI(Uri.parse(android.resource://+ getPackageName()
                        +/+ R.raw.b));
         vid.setMediaController(新的MediaController(My_videos.this));
             vid.requestFocus();
             vid.start(); }     否则如果(night.equalsIgnoreCase(按钮2)){
        vid.setVideoURI(Uri.parse(android.resource://+ getPackageName()
                       +/+ R.raw.bb));
         vid.setMediaController(新的MediaController(My_videos.this));
             vid.requestFocus();
             vid.start(); }     否则如果(night.equalsIgnoreCase(按钮3)){
     vid.setVideoURI(Uri.parse(android.resource://+ getPackageName()
                    +/+ R.raw.bbb));
         vid.setMediaController(新的MediaController(My_videos.this));
             vid.requestFocus();
             vid.start(); }     否则如果(night.equalsIgnoreCase(将Button4)){
     vid.setVideoURI(Uri.parse(android.resource://+ getPackageName()
                    +/+ R.raw.bbbb));
         vid.setMediaController(新的MediaController(My_videos.this));
             vid.requestFocus();
             vid.start(); }     否则如果(night.equalsIgnoreCase(button5)){
     vid.setVideoURI(Uri.parse(android.resource://+ getPackageName()
                    +/+ R.raw.bbbbb));
         vid.setMediaController(新的MediaController(My_videos.this));
             vid.requestFocus();
             vid.start(); }                                      }}


解决方案

您在设置不正确的听众。为了使听众工作,你必须点击的ListView 排一次,以实际设置的侦听器按钮的TextView 。正确的方法是设置听众在 getView 方法,当你布局的意见。

先删除code你在 onListItemClick 回调有,然后修改适配器。我已经重写你的适配器,更高效的版本(虽然越来越多的可以改进)。由于code是大,你可以在这里找到新的适配器 https://gist.github.com/3463215 。看看这是你想要的。

R.layout.video 布局文件:

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直><的LinearLayout的android:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT>
<按钮
    机器人:ID =@ + ID /按钮1
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:文字=一/><按钮
    机器人:ID =@ + ID /按钮2
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
     机器人:文字=两课/><按钮
    机器人:ID =@ + ID /按钮3
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:文字=三/><按钮
    机器人:ID =@ + ID /将Button4
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:文字=四/><按钮
    机器人:ID =@ + ID / button5
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:文字=十二五/>< / LinearLayout中>< VideoView
    机器人:ID =@ + ID / videoView1
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT/>< / LinearLayout中>

i have a listactivity app , each row cotain TEXT and BUTTON , both text and button must be clickable (what im try to get it ) , when click the text it will open MyDay activity and when click button it will open My_videos activity .

whats happen exactly when you open the app and click on any button , it do no click action in any button in all rows , but when you click any row text so it will open MyDay activity then click any button in the first row ONLY , it will open My_videos activity which is videoview , but in the same time the other buttons is not clickable in the rest of rows .

any advice will be appreciated ,

THANKS.

MY CODE :

MyArrayAdapter Class:

 public class MyArrayAdapter extends ArrayAdapter<String> {
private final Activity context;
private final String[] classes;
Button bt1, bt2, bt3, bt4, bt5;   

Typeface tf;

public MyArrayAdapter(Activity context, String[] classes) {
    super(context, R.layout.row, classes);
    this.context = context;
    this.classes = classes;
              }

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    tf=Typeface.createFromAsset(context.getAssets(),"BFantezy.ttf"); 
    LayoutInflater inflater = context.getLayoutInflater();
    View rowView = inflater.inflate(R.layout.row, null, true);
    TextView textView = (TextView) rowView.findViewById(R.id.row_label);
    Button bt1=(Button) rowView.findViewById(R.id.button1); 
    Button bt2=(Button) rowView.findViewById(R.id.button2); 
    Button bt3=(Button) rowView.findViewById(R.id.button3); 
    Button bt4=(Button) rowView.findViewById(R.id.button4); 
    Button bt5=(Button) rowView.findViewById(R.id.button5); 

    String s = classes[position];
    textView.setText(s);
    ((TextView)textView).setTypeface(tf); 


if ( s.startsWith("First")) {

    bt1.setBackgroundResource(R.drawable.ic_launcher);
    bt2.setBackgroundResource(R.drawable.ic_launcher); 
    bt3.setBackgroundResource(R.drawable.ic_launcher); 
    bt4.setBackgroundResource(R.drawable.ic_launcher); 
    bt5.setBackgroundResource(R.drawable.ic_launcher); }

if ( s.startsWith("Second")) {

    bt1.setBackgroundResource(R.drawable.ic_launcher); 
    bt2.setBackgroundResource(R.drawable.ic_launcher); 
    bt3.setBackgroundResource(R.drawable.ic_launcher); 
    bt4.setBackgroundResource(R.drawable.ic_launcher); 
    bt5.setBackgroundResource(R.drawable.ic_launcher); }    

if ( s.startsWith("Third")) {

    bt1.setBackgroundResource(R.drawable.ic_launcher); 
    bt2.setBackgroundResource(R.drawable.ic_launcher); 
    bt3.setBackgroundResource(R.drawable.ic_launcher); 
    bt4.setBackgroundResource(R.drawable.ic_launcher); 
    bt5.setBackgroundResource(R.drawable.ic_launcher); }        

if ( s.startsWith("Fourth")) {

    bt1.setBackgroundResource(R.drawable.ic_launcher); 
    bt2.setBackgroundResource(R.drawable.ic_launcher); 
    bt3.setBackgroundResource(R.drawable.ic_launcher); 
    bt4.setBackgroundResource(R.drawable.ic_launcher); 
    bt5.setBackgroundResource(R.drawable.ic_launcher); }    

if ( s.startsWith("Fifth")) {

    bt1.setBackgroundResource(R.drawable.ic_launcher); 
    bt2.setBackgroundResource(R.drawable.ic_launcher); 
    bt3.setBackgroundResource(R.drawable.ic_launcher); 
    bt4.setBackgroundResource(R.drawable.ic_launcher); 
    bt5.setBackgroundResource(R.drawable.ic_launcher); }        

return rowView;  }}

My_videos Class:

  public class My_videos extends Activity {  
private VideoView vid;  
String night;  
/** Called when the activity is first created. */  
@Override  
public void onCreate(Bundle savedInstanceState) {   
    super.onCreate(savedInstanceState);   
    setContentView(R.layout.video);    

    Bundle bdl = getIntent().getExtras(); 
    night = bdl.getString("video");   

    vid = (VideoView) findViewById(R.id.videoView1); 

      if (night.equalsIgnoreCase("button1")) {    
         vid.setVideoURI(Uri.parse("android.resource://" + getPackageName()
                        + "/" + R.raw.b));
         vid.setMediaController(new MediaController(My_videos.this));  
             vid.requestFocus();    
             vid.start(); }  

     else if (night.equalsIgnoreCase("button2")) {   
        vid.setVideoURI(Uri.parse("android.resource://" + getPackageName() 
                       + "/" + R.raw.bb)); 
         vid.setMediaController(new MediaController(My_videos.this));
             vid.requestFocus(); 
             vid.start(); } 

     else if (night.equalsIgnoreCase("button3")) { 
     vid.setVideoURI(Uri.parse("android.resource://" + getPackageName() 
                    + "/" + R.raw.bbb)); 
         vid.setMediaController(new MediaController(My_videos.this));
             vid.requestFocus();
             vid.start(); } 

     else if (night.equalsIgnoreCase("button4")) { 
     vid.setVideoURI(Uri.parse("android.resource://" + getPackageName() 
                    + "/" + R.raw.bbbb)); 
         vid.setMediaController(new MediaController(My_videos.this)); 
             vid.requestFocus();     
             vid.start(); } 

     else if (night.equalsIgnoreCase("button5")) {   
     vid.setVideoURI(Uri.parse("android.resource://" + getPackageName()  
                    + "/" + R.raw.bbbbb)); 
         vid.setMediaController(new MediaController(My_videos.this));   
             vid.requestFocus();      
             vid.start(); }  

                                      } } 

解决方案

You are setting the listeners incorrect. In order for the listeners to work you would have to click the ListView row once to actually set the listeners for the Buttons and TextView. The correct way would be to set the listeners in the getView method when you layout the views.

First delete the code you have in the onListItemClick callback and then modify the adapter. I've rewritten your adapter to a more efficient version(although more improvements could be made). As the code is big you can find the new adapter here https://gist.github.com/3463215 . See if this is what you want.

The R.layout.video layout file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<LinearLayout  android:layout_width="match_parent"
    android:layout_height="wrap_content" >
<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="One" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
     android:text="Two" />

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="three" />

<Button
    android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Four" />

<Button
    android:id="@+id/button5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Five" />

</LinearLayout>

<VideoView
    android:id="@+id/videoView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

</LinearLayout>

这篇关于Listactivity文字和按钮单击奇怪的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆