我创建了一个使用ScrollView的活动。在ScrollView内部,我创建了一个带有两个子布局的ViewFlipper的LinearLayout,并将ViewFlipper设置为“不可见”。在ViewFlipper之前我有两个按钮,我想设置我的应用程序来加载ViewFlipper的右子,具体取决于用户按下的按钮。 ViewFlipper的子布局带有按钮。我按下时让他们在阵列中添加他们的ID,我想让他们处于按下状态,直到用户再次按下它们才能释放它们。 我差不多完成了,但我遇到了两个问题:
1)当按下按钮时,ViewFlipper绘制相对布局,但是在我按下另一个按钮并且ViewFlipper将绘制另一个布局之前,onTouchListener不起作用。然后在更改布局时看起来它正常工作。有没有人知道如何在不切换布局的情况下立即使其工作?
2)onTouchListener无法完全正常工作。我的意思是,我想设置button.setPressed(true)或false,如果已经设置为true,但看起来它是随机的,如果它将正常工作。有时,我按下一个按钮,没有任何反应,几秒钟后我再试一次,工作正常。有谁知道我怎么解决它或者建议一个更好的方法让按钮保持按下状态?
我的活动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());
}
}
答案 0 :(得分:0)
我终于使用了片段而不是ViewFlipper和toggleButtons而不是简单的按钮而且工作正常!