我创建了一个使用 ScrollView 的活动。在 ScrollView 中,我创建了一个带有两个子布局的 ViewFlipper 的 LinearLayout,并将 ViewFlipper 设置为“不可见”。在 ViewFlipper 之前,我有两个按钮,我想将我的应用程序设置为根据用户按下的按钮加载 ViewFlipper 的右子项。ViewFlipper 的子布局带有按钮。我让他们在按下时将他们的 ID 添加到一个数组中,我想让他们处于按下状态,直到用户再次按下它们以释放它们。我几乎做到了,但我遇到了两个问题:
1)当按下按钮时,ViewFlipper 绘制相对布局,但 onTouchListener 直到我按下另一个按钮后才会起作用,ViewFlipper 将绘制另一个布局。然后它看起来在更改布局时工作正常。有谁知道如何让它立即工作而不在布局之间切换?
2) onTouchListener 不能完全正常工作。我的意思是,如果已经设置为 true,我想设置 button.setPressed(true) 或 false,但如果它可以正常工作,它看起来是随机的。有时,我按下一个按钮并没有任何反应,几秒钟后我再试一次并且工作正常。有谁知道我该如何解决它或必须建议一种更好的方法来使按钮保持按下状态?
我的活动 xml 文件是:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ScrollView
android:id="@+id/ScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/formLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="@+id/selectTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Select an option" />
<LinearLayout
android:id="@+id/chooseButtons"
android:layout_width="wrap_content"
android:layout_height="120dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_gravity="center_horizontal"
android:orientation="horizontal" >
<Button
android:id="@+id/firstBtn"
android:layout_width="120dp"
android:layout_height="fill_parent"
android:layout_marginBottom="1dp"
android:layout_marginTop="2dp"
android:layout_marginRight="5dp"
android:text="Option 1"
android:textColor="@android:color/white"
android:background="@drawable/option1"/>
<Button
android:id="@+id/secondBtn"
android:layout_width="120dp"
android:layout_height="fill_parent"
android:layout_marginBottom="1dp"
android:layout_marginTop="2dp"
android:layout_marginLeft="5dp"
android:text="Option 2"
android:textColor="@android:color/white"
android:background="@drawable/option2"/>
</LinearLayout>
<TextView
android:id="@+id/subChoices"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="20dp"
android:layout_gravity="center_horizontal"
android:text="Select 5 options" />
<ViewFlipper
android:id="@+id/optionsFlipper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible">
<include android:id="@+id/Include1" layout="@layout/option1layout" />
<include android:id="@+id/Include2" layout="@layout/option2layout" />
</ViewFlipper>
<Button
android:id="@+id/okBtn"
android:text="OK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"/>
</LinearLayout>
</ScrollView>
这是我的java代码:
public class OptionsSelection extends Activity
{
protected Button ok, option1, option2;
protected Button subOption1, subOption2, subOption3, subOption4, subOption5;
protected ViewFlipper optionsFlipper;
protected int optionID;
protected boolean status, exists;
protected int[] subOptions = new int[3];
protected int counter=0;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.options);
option1 = (Button) findViewById(R.id.firstBtn);
option2 = (Button) findViewById(R.id.secondBtn);
ok = (Button) findViewById(R.id.okBtn);
optionsFlipper = (ViewFlipper)findViewById(R.id.optionsFlipper);
option2.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if (optionID == 2)
{
for(int i=0;i<subOptions.length;i++)
subOptions[i] = 20;
}
exists = false;
optionID = 1;
counter = 0;
optionsFlipper.setDisplayedChild(1);
subOption1 = (Button) findViewById(R.id.option1SubOption1);
subOption2 = (Button) findViewById(R.id.option1SubOption2);
subOption3 = (Button) findViewById(R.id.option1SubOption3);
subOption4 = (Button) findViewById(R.id.option1SubOption4);
subOption5 = (Button) findViewById(R.id.option1SubOption5);
setOnTouchListeners();
optionsFlipper.setVisibility(View.VISIBLE);
status = false;
}
});
option1.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if (optionID == 1)
{
for(int i=0;i<subOptions.length;i++)
{
subOptions[i] = 20;
}
}
exists = false;
optionID = 2;
counter = 0;
optionsFlipper.setDisplayedChild(2);
subOption1 = (Button) findViewById(R.id.option1SubOption1);
subOption2 = (Button) findViewById(R.id.option1SubOption2);
subOption3 = (Button) findViewById(R.id.option1SubOption3);
subOption4 = (Button) findViewById(R.id.option1SubOption4);
subOption5 = (Button) findViewById(R.id.option1SubOption5);
setOnTouchListeners();
optionsFlipper.setVisibility(View.VISIBLE);
status = false;
}
});
ok.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
for(int i=0;i<subOptions.length;i++)
{
System.out.println(subOptions[i]);
}
}
});
}
public class onTouchListener implements View.OnTouchListener
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
if (status == true)
{
v.setSelected(false);
v.setPressed(false);
status=false;
return true;
}
else if (status == false)
{
v.setSelected(true);
v.setPressed(true);
status=true;
/*getting subOption button tag and convert it to string in order to remove the first 6 characters*/
String buttonClicked = v.getTag().toString();
int buttonID = Integer.parseInt(buttonClicked.substring(6));
switch (buttonID)
{
case 1:
{
System.out.println("Case 1!");
if (optionID == 1)
addToArray(subOptions, 1);
else if (optionID == 2)
addToArray(subOptions, 6);
else
System.out.println("Error occurred in assign of key");
}
break;
case 2:
{
if (optionID == 1)
addToArray(subOptions, 2);
else if (optionID == 2)
addToArray(subOptions, 7);
else
System.out.println("Error occurred in assign of key");
}
break;
case 3:
{
if (optionID == 1)
addToArray(subOptions, 3);
else if (optionID == 2)
addToArray(subOptions, 8);
else
System.out.println("Error occurred in assign of key");
}
break;
case 4:
{
if (optionID == 1)
addToArray(subOptions, 4);
else if (optionID == 2)
addToArray(subOptions, 9);
else
System.out.println("Error occured in assign of key");
}
break;
case 5:
{
if (optionID == 1)
addToArray(subOptions, 5);
else if (optionID == 2)
addToArray(subOptions, 10);
else
System.out.println("Error occured in assign of key");
}
break;
}
System.out.println("Pressed changed to true");
return true;
}
else
return true;
}
};
public void addToArray(int[] ar, int a)
{
for (int i=0;i<ar.length;i++)
{
if (ar[i] == a)
exists = true;
System.out.println("Already exists");
}
if((counter<=3)&&(exists==false))
{
ar[counter] = a;
counter++;
}
else if ((counter>3)&&(exists==false))
{
counter = 0;
ar[counter] = a;
counter++;
}
else
{
System.out.println("Already Exists...");
}
System.out.println("Added new integer to Array");
}
public void setOnTouchListeners()
{
subOption1.setOnTouchListener(new onTouchListener());
subOption2.setOnTouchListener(new onTouchListener());
subOption3.setOnTouchListener(new onTouchListener());
subOption4.setOnTouchListener(new onTouchListener());
subOption5.setOnTouchListener(new onTouchListener());
}
}