相关疑难解决方法(0)

使用Switch语句处理按钮单击

我正试图围绕视图,听众等.我有一个带有2个按钮的活动:按钮和按钮停止.我的问题是我无法完全绕过Views和Listener来生成一个有效的switch语句.

例如,我想创建一个SINGLE Listener并以某种方式使用它来确定单击了哪个按钮.然后以某种方式使用我的switch语句点​​击按钮的ID,但是,一切都我在网上找似乎使用单独的侦听器为每个按钮,然后以某种方式使用该视图作为参数传递给switch语句.

我意识到下面的代码不正确,但我正在寻找完成上述操作所需的更改.

我想根据点击的按钮来控制MediaPlayer.我有:

   Button b1 = (Button) findViewById(R.id.buttonplay);       
    b1.setOnClickListener(new View.OnClickListener()         
    {

        public void onClick(View v) {
           // Perform action on click
          switch(v.getId()) {
          case R.id.buttonplay:
          //Play voicefile
          MediaPlayer.create(getBaseContext(), R.raw.voicefile).start();
          break;
          case R.id.buttonstop:
          //Stop MediaPlayer
              MediaPlayer.create(getBaseContext(), R.raw.voicefile).stop();
          break;
         }
   }
    });
Run Code Online (Sandbox Code Playgroud)

最终,我希望以最直接的方式打开点击的任何按钮.我相信我的困惑很大一部分源于在此上下文中使用onClickListeners和Views的方式.

}

android button listener

15
推荐指数
3
解决办法
10万
查看次数

如何为整个屏幕设置 OnTouchListener?

请看下面的代码。我只发布代码的重要部分。

活动游戏.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fullView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Game" >

    <TextView
        android:id="@+id/numberOfQuestionsLeft"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="20dp"
        android:text="TextView" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

游戏.java

public class Game extends Activity {



    private View fullView;

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

        fullView = (View)findViewById(R.id.fullView);
        fullView.setOnTouchListener(imageViewSwiped);



    }


 OnTouchListener imageViewSwiped = new OnSwipeTouchListener()
    {
         public boolean onSwipeRight() 
         {
            //code removed
         }

          public boolean onSwipeLeft() 
          {
             //code removed
                return true;
          }

          public boolean onSwipeBottom()
          {
              //code removed
              return true;
          }


    };
} …
Run Code Online (Sandbox Code Playgroud)

java android touch ontouchlistener android-activity

5
推荐指数
2
解决办法
2万
查看次数

如何将属性onClick设置为ScrollView?

我有以下布局

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/white" >

<LinearLayout
    android:id="@+id/answerMainFrame"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:isScrollContainer="true"
    android:onClick="toQuestion" >

    <ImageView
        android:id="@+id/answer_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:contentDescription="@string/question_img_cd" />

    <TextView
        android:id="@+id/answer"
        style="@style/Question" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

有时ScrollView它太小而且不会填满整个屏幕,但我仍然想要toQuestion()点击屏幕上任意位置的方法.

我试过设置android:layout_height="wrap_content"LinearLayout,并android:onClick="toQuestion"ScrollView,但相同的结果.

android onclick scrollview

2
推荐指数
1
解决办法
6318
查看次数