在我的代码中,我想将 TextView 文本名称显示为Toast消息。 我做了以下编码。我没有得到我所得到的文本的正确文本对象。
在 Toast 我想显示文字,请尽可能指导我。 这是我的代码:
package com.Viewflipper;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.widget.ViewFlipper;
public class Viewflipper extends Activity implements OnClickListener {
/** Called when the activity is first created. */
Button next;
Button previous;
ViewFlipper vf;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
vf = (ViewFlipper) findViewById(R.id.ViewFlipper01);
next = (Button) findViewById(R.id.Button01);
previous = (Button) findViewById(R.id.Button02);
next.setOnClickListener(this);
previous.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == next) {
vf.showNext();
Toast.makeText(this,vf.toString(), Toast.LENGTH_SHORT).show();
**//HERE I DIDN'T GET vf.getText().toString();**
}
if (v == previous) {
vf.showPrevious();
}
}
}
**EDITED**
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical">
<LinearLayout android:id="@+id/LinearLayout03"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<Button android:id="@+id/Button01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Next"></Button>
<Button android:id="@+id/Button02" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Previous"></Button>
</LinearLayout>
<LinearLayout android:id="@+id/LinearLayout02"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<ViewFlipper android:id="@+id/ViewFlipper01"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<!--adding views to ViewFlipper-->
<TextView android:id="@+id/TextView01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="@drawable/a10"
android:text="one"></TextView>
<TextView android:id="@+id/TextView02" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="@drawable/a11"
android:text="two"></TextView>
<TextView android:id="@+id/TextView03" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="@drawable/a12"
android:text="three">
</TextView>
<TextView android:id="@+id/TextView04" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="@drawable/a13"></TextView>
<TextView android:id="@+id/TextView05" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="@drawable/a14"></TextView>
<TextView android:id="@+id/TextView06" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="@drawable/a15"></TextView>
</ViewFlipper>
</LinearLayout>
</LinearLayout>
答案 0 :(得分:1)
试试这个: -
package com.Viewflipper;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.widget.ViewFlipper;
public class Viewflipper extends Activity implements OnClickListener {
/** Called when the activity is first created. */
Button next;
Button previous;
ViewFlipper vf;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
vf = (ViewFlipper) findViewById(R.id.ViewFlipper01);
next = (Button) findViewById(R.id.Button01);
previous = (Button) findViewById(R.id.Button02);
next.setOnClickListener(this);
previous.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == next) {
vf.showNext();
Toast.makeText(getApplicationContext(),((TextView) vf.getCurrentView()).getText(), Toast.LENGTH_SHORT).show();
}
if (v == previous) {
vf.showPrevious();
}
}
}
答案 1 :(得分:0)
ViewFliper是一个适配器..您不能像这样抓住它,您必须与适配器交互并获取视图,然后然后抓取该视图中的textview。