gpt4 book ai didi

java - 没有按钮单击的TextSwitcher

转载 作者:行者123 更新时间:2023-12-03 07:32:22 24 4
gpt4 key购买 nike

嗨,有人可以帮我,因为我是android studio开发的新手,需要帮助。我已经创建了一个带有按钮单击的textswitcher,但是我想添加一些修改,例如当用户建立联系或在屏幕上滑动时,它会滑动到下一个文本,而不是使用click on按钮。请您指教?

Java文件

public class NationalMediaMuseum extends Activity
{
private TextSwitcher mSwitcher;
Button btnNext;

// Array of String to Show In TextSwitcher
String textToShow[]={"Main HeadLine","Hello World ","Hello Eeveryone","New Articles","Business News","What IS New"};
int messageCount=textToShow.length;
// to keep current Index of text
int currentIndex=-1;



@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_national_media_museum);

// get The references
btnNext=(Button)findViewById(R.id.buttonNext);
mSwitcher = (TextSwitcher) findViewById(R.id.textSwitcher);

// Set the ViewFactory of the TextSwitcher that will create TextView object when asked
mSwitcher.setFactory(new ViewSwitcher.ViewFactory() {

public View makeView() {
// TODO Auto-generated method stub
// create new textView and set the properties like clolr, size etc
TextView myText = new TextView(NationalMediaMuseum.this);
myText.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
myText.setTextSize(36);
myText.setTextColor(Color.BLUE);
return myText;
}
});

// Declare the in and out animations and initialize them
Animation in = AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left);
Animation out = AnimationUtils.loadAnimation(this,android.R.anim.slide_out_right);

// set the animation type of textSwitcher
mSwitcher.setInAnimation(in);
mSwitcher.setOutAnimation(out);

// ClickListener for NEXT button
// When clicked on Button TextSwitcher will switch between texts
// The current Text will go OUT and next text will come in with specified animation
btnNext.setonclickListener(new View.onclickListener() {

public void onclick(View v) {
// TODO Auto-generated method stub
currentIndex++;
// If index reaches maximum reset it
if (currentIndex == messageCount)
currentIndex = 0;
mSwitcher.setText(textToShow[currentIndex]);
}
});

}
}

XML文件
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:orientation="vertical">

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1"
android:background="#0d47a1" >

<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/imageView8"
android:src="@drawable/logo"
android:layout_weight="0.32"
android:contentDescription="@string/logonew" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/logo1"
android:textColor="#ffffff"
android:id="@+id/textView12"
android:layout_gravity="center"
android:textSize="30sp"
android:layout_marginLeft="25dp" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/imageView9"
android:src="@drawable/logo"
android:layout_weight="1"
android:contentDescription="@string/logo2" />
</LinearLayout>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="801dp"
android:background="#64b5f6"
android:id="@+id/relativeLayout3">

<TextView
android:layout_width="200dp"
android:textColor="#ffffffff"
android:layout_height="40dp"
android:id="@+id/textwelcome"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<TextView
android:layout_width="125dp"
android:layout_height="40dp"
android:id="@+id/uname"
android:layout_alignParentTop="true"
android:textColor="#ffffffff"
android:layout_toRightOf="@+id/textwelcome"
android:textStyle="italic"
android:layout_toStartOf="@+id/logout"
android:layout_toLeftOf="@+id/logout" />


<Button
android:layout_width="80dp"
android:layout_height="40dp"
android:text="LOGOUT"
android:id="@+id/logout"
android:textColor="#ffffffff"
android:background="#0d47a1"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Museum And Theatres Category"
android:textColor="#ffffff"
android:id="@+id/textView16"
android:layout_gravity="center"
android:textSize="20dp"
android:gravity="center|center_horizontal"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/textwelcome"
android:layout_alignRight="@+id/logout"
android:layout_alignEnd="@+id/logout" />

<Button
android:layout_width="80dp"
android:layout_height="40dp"
android:text="NEXT"
android:id="@+id/nxtbtn"
android:textColor="#ffffffff"
android:background="#0d47a1"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />

<Button
android:layout_width="80dp"
android:layout_height="40dp"
android:text="BACK"
android:id="@+id/bkebtn"
android:textColor="#ffffffff"
android:background="#0d47a1"
android:layout_alignTop="@+id/nxtbtn"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<Button
android:layout_width="80dp"
android:layout_height="40dp"
android:text="HOME"
android:id="@+id/hmebtn"
android:textColor="#ffffffff"
android:background="#0d47a1"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />

<TextSwitcher
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textSwitcher"
android:layout_below="@+id/textView16"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<Button
android:layout_width="80dp"
android:layout_height="40dp"
android:text="NEXT"
android:id="@+id/buttonNext"
android:textColor="#ffffffff"
android:background="#0d47a1"
android:layout_below="@+id/textSwitcher"
android:layout_alignLeft="@+id/hmebtn"
android:layout_alignStart="@+id/hmebtn"
android:layout_marginTop="61dp" />

</RelativeLayout>
</LinearLayout>

最佳答案

这是一个指向android开发人员页面的链接,该页面提供了有关如何使用ViewPager的详细信息,该ViewPager应该可以完全满足您的要求。这是我以前在应用程序中完成的方法。 ViewPager

关于java - 没有按钮单击的TextSwitcher,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32139269/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com