如何使TextView可以滑动并在其中显示第二个TextView

时间:2018-05-15 12:11:22

标签: android textview

我在页面上有5个不同的TextView,我想让它们可以滑动,这样当我向左滑动它们时,它会显示相同的信息但具有不同的值(例如Name = Ben,但是当向左滑动Name = Tim时,这可能吗?如果是这样我怎么去做呢? TextView的信息是从Firebase数据库中提取的,但如果需要,可以更改为静态值。

这是我到目前为止所做的:

CalendarActivity:

package com.example.adamf.authtest;

import android.content.Intent;
import android.renderscript.Allocation;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.firebase.FirebaseApp;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import org.w3c.dom.Text;

import android.text.util.Linkify;

import java.util.GregorianCalendar;

public class CalendarActivity extends AppCompatActivity implements View.OnClickListener {

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String uid = user.getUid();



DatabaseReference mRootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference mUserRef = mRootRef.child(uid).child("2018-04-23").child("Jane Jenkins");
DatabaseReference mNameRef = mUserRef.child("Name");
DatabaseReference mAddressRef = mUserRef.child("Address");
DatabaseReference mPostCodeRef = mUserRef.child("PostCode");
DatabaseReference mMobileRef = mUserRef.child("Mobile");
DatabaseReference mTimeRef = mUserRef.child("Time");

private DatabaseReference mDatabase;

private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;


private Button InformationBtn = null;
TextView mName;
TextView mAddress;
TextView mPostCode;
TextView mMobile;
TextView mTime;


@Override
protected void onDestroy() {
    super.onDestroy();
    mAuth.signOut();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.buttonSignOut) {
        startActivity(new Intent(CalendarActivity.this, LoginActivity.class));
        FirebaseAuth.getInstance().signOut();
        Toast.makeText(CalendarActivity.this, "Sign out successful", Toast.LENGTH_LONG).show();
    }

    return super.onOptionsItemSelected(item);
}

@Override
public void onBackPressed() {
    Toast.makeText(getApplicationContext(), "Please use the Sign Out button above", Toast.LENGTH_SHORT).show();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_calendar_v2);

    mName = findViewById(R.id.textViewName);
    mAddress = findViewById(R.id.textViewAddress);
    mPostCode = findViewById(R.id.textViewPostcode);
    mMobile = findViewById(R.id.textViewMobile);
    mTime = findViewById(R.id.textViewTime);

    InformationBtn = findViewById(R.id.buttonInformation);
    InformationBtn.setOnClickListener(this);

    mAuth = FirebaseAuth.getInstance();


    Linkify.addLinks(mMobile, Linkify.PHONE_NUMBERS);
    Linkify.addLinks(mAddress, Linkify.MAP_ADDRESSES);



    InformationBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(CalendarActivity.this, ClientInfo.class));

        }
    });



    CalendarView view = new CalendarView(this);
    view.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
        public GregorianCalendar calendar;

        @Override
        public void onSelectedDayChange(CalendarView calendarView, int year, int month, int day) {
            this.calendar = new GregorianCalendar(year, month, day);
        }
    });











}

protected void onStart() {

    super.onStart();

    mNameRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String text = dataSnapshot.getValue(String.class);
            mName.setText(text);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

    mAddressRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String text = dataSnapshot.getValue(String.class);
            mAddress.setText(text);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

    mPostCodeRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String text = dataSnapshot.getValue(String.class);
            mPostCode.setText(text);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

    mMobileRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String text = dataSnapshot.getValue(String.class);
            mMobile.setText(text);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

    mTimeRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String text = dataSnapshot.getValue(String.class);
            mTime.setText(text);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });












}




@Override
public void onClick(View v) {

}


}

activity_calendar_v2.xml(布局文件):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools">

<CalendarView
    android:id="@+id/calendar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:minWidth="400dp" />



<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textName"
    android:layout_below="@+id/calendar"
    android:text="Name of patient:"
    android:layout_marginTop="20dp"
    android:layout_marginStart="30dp"
    android:textSize="14dp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textViewName"
    android:layout_toEndOf="@+id/textName"
    android:layout_alignBottom="@+id/textName"
    android:text=" "
    android:textSize="12dp"
    android:layout_marginStart="20dp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textAddress"
    android:layout_below="@+id/textName"
    android:text="Patients address:"
    android:layout_marginTop="10dp"
    android:layout_marginStart="30dp"
    android:autoLink="map"
    android:textSize="14dp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textViewAddress"
    android:text=" "
    android:textSize="12dp"
    android:layout_alignStart="@+id/textViewName"
    android:layout_alignBottom="@+id/textAddress"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textPostcode"
    android:layout_below="@+id/textAddress"
    android:text="Postcode:"
    android:layout_marginTop="10dp"
    android:layout_marginStart="30dp"
    android:textSize="14dp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textViewPostcode"
    android:text=" "
    android:textSize="12dp"
    android:layout_alignStart="@+id/textViewName"
    android:layout_alignBottom="@+id/textPostcode"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textMobile"
    android:layout_below="@+id/textPostcode"
    android:text="Mobile number:"
    android:layout_marginTop="10dp"
    android:layout_marginStart="30dp"
    android:autoLink="phone"
    android:linksClickable="true"
    android:textSize="14dp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textViewMobile"
    android:text=" "
    android:textSize="12dp"
    android:layout_alignStart="@+id/textViewName"
    android:layout_alignBottom="@+id/textMobile"
    android:autoLink="phone"
    android:linksClickable="true"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textTime"
    android:layout_below="@+id/textMobile"
    android:text="Appointment time:"
    android:layout_marginTop="10dp"
    android:layout_marginStart="30dp"
    android:textSize="14dp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textViewTime"
    android:text=" "
    android:textSize="12dp"
    android:layout_alignStart="@+id/textViewName"
    android:layout_alignBottom="@+id/textTime"/>



<Button
    android:id="@+id/buttonInformation"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textTime"
    android:text="View Patient Information"
    android:textSize="12dp"
    android:layout_marginTop="10dp"
    android:layout_centerHorizontal="true"/>

</RelativeLayout>

我是否还必须将所有TextView内容放在一起将它们组合在一起?

由于

0 个答案:

没有答案