您好我是Android应用中的新手,所以我希望得到您的帮助。
所以基本上我要做的是将textview的内容从“Read more”更改为“Read less”,然后当我再次单击以更改回“Read more”时。然后我的第二个问题是当我点击“阅读更多”时,如何从主要活动(java而不是xml)更改我的textview位置,再次点击它再次返回到原始位置时。
这是我的主要活动:
TextView paragraphmore;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
paragraphmore = (TextView) findViewById(R.id.paragraphmore);
// hide until its title is clicked
paragraphmore.setVisibility(View.GONE);
}
public void toggle_contents(View v){
paragraphmore.setVisibility(paragraphmore.isShown()
? View.GONE
: View.VISIBLE );
TextView view = (TextView) findViewById(R.id.readbutton);
view.setText("Read less");
}
这些是textviews:
<TextView
android:id="@+id/readbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/readbutton"
android:clickable="true"
android:onClick="toggle_contents"
android:layout_marginTop="15dp"
android:textColor="#FFFFFF"
android:textSize="11sp">
</TextView>
<!--content to hide/show -->
<TextView android:id="@+id/paragraphmore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/paragraphmore"
android:layout_marginTop="15dp"
android:textColor="#FFFFFF"
android:textSize="11sp">
</TextView>
答案 0 :(得分:0)
试试这个..
Log.v("View.VISIBLE", ""+View.VISIBLE); //0
Log.v("View.GONE", ""+View.GONE); //8
Log.v("visible", ""+paragraphmore.getVisibility());
if(paragraphmore.getVisibility() == 0){
paragraphmore.setVisibility(View.VISIBLE);
view.setText("Read Less");
}else{
paragraphmore.setVisibility(View.GONE);
view.setText("Read More");
}
答案 1 :(得分:0)
1)将textview的内容从“Read more”更改为“Read less”。我想你需要设置一个布尔变量(例如私有布尔值readMore)来监听textview的行为。在段落更多按钮的xml“android:onClick”属性中设置:
public void onTextClick(View v){ readMore =!readMore; if(readMore)readMore(); else readLess(); }
在readMore()方法中,将textview的文本设置为“Read more”,在readLess()方法中,将textview的文本设置为“Read less”。
2)当我点击“阅读更多”时,如何再次点击再次返回原始位置时,如何从我的主要活动(java而不是xml)更改我的textview位置。
例如,您可以设置一个Point(或2 int)(如果使用所有4个边距,或只是int [])变量,则设置一个Point来保存视图的先前边距。 ......那样:
MarginLayoutParams params = (MarginLayoutParams) view.getLayoutParams(); //http://developer.android.com/reference/android/view/ViewGroup.MarginLayoutParams.html
margins[0] = params.leftMargin;
margins[1] = params.topMargin; //and so for [2] and [3]
然后在onTextClick方法中,将视图的当前边距保存到局部变量,并将新视图的边距设置为先前保存的边距,并将先前保存的边距更改为局部变量内容:
public void onTextClick(View v) {
//...
int[] newMargins = new int[4];
MarginLayoutParams params = (MarginLayoutParams) view.getLayoutParams();
newMargins[0] = params.leftMargin;// and so on...
params.setMargins(margins[0], margins[1], margins[2], margins[3]);
margins = newMargins;
}
希望有所帮助:)
答案 2 :(得分:0)
您可以在TextView上添加Click侦听器,并在点击时比较GravityFlags(原谅我的c#/ mono语法):
var readbutton = new TextView(this)
{
Text = "positon",
LayoutParameters =
new ViewGroup.LayoutParams(200, ViewGroup.LayoutParams.WrapContent)
};
readbutton.Gravity = GravityFlags.Left;
readbutton.Click += (sender, args) =>
{
var tvGravity = readbutton.Gravity & GravityFlags.Right;
readbutton.Gravity = tvGravity == GravityFlags.Right ? GravityFlags.Left : GravityFlags.Right;
};
其中readbutton
是您希望更改
答案 3 :(得分:0)
我找到了答案。我知道使用绝对定位或边距布局对我的应用程序没有好处,因为那时我会遇到问题,当应用程序从一个设备转到另一个具有不同分辨率的设备时。所以我创建了一个新的可点击文本视图,我可以管理它的位置,然后我将主要活动改为我的toggle_content方法,以便首先使不可见的文本视图变为不可见,然后在单击它后查看更多的textview。
TextView paragraphmore;
TextView readbutton;
TextView readbutton1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
paragraphmore = (TextView) findViewById(R.id.paragraphmore);
// hide until its title is clicked
paragraphmore.setVisibility(View.GONE);
readbutton1 = (TextView) findViewById(R.id.readbutton1);
// hide until its title is clicked
readbutton1.setVisibility(View.GONE);
readbutton = (TextView) findViewById(R.id.readbutton);
readbutton.setVisibility(View.VISIBLE);
}
public void toggle_contents(View v){
Log.v("View.VISIBLE", ""+View.VISIBLE);
Log.v("View.GONE", ""+View.GONE);
Log.v("visible", ""+paragraphmore.getVisibility());
paragraphmore.setVisibility(paragraphmore.isShown()
? View.GONE
: View.VISIBLE );
if(paragraphmore.getVisibility() == 0){
paragraphmore.setVisibility(View.VISIBLE);
readbutton1.setVisibility(View.VISIBLE);
readbutton.setVisibility(View.GONE);
}
else{
readbutton.setVisibility(View.VISIBLE);
paragraphmore.setVisibility(View.GONE);
readbutton1.setVisibility(View.GONE);
}
}