如何在Android中实现此功能

时间:2014-03-24 06:36:23

标签: android

我是Android的新手。任何人都可以告诉我它是什么类型的技术?我想在密码字段中添加此功能。我可以知道实现目标的正确方法是什么?您能否帮助我解决任何代码或指南如何正确实施?

1 个答案:

答案 0 :(得分:2)

您有很多可能来创建这种布局:

  • 父亲RelativeLayoutLinearLayout顶部有4个点,底部显示您的the attributes视图,其中包括LeftOf,toRightOf等。 - 制作{{1} }方法来改变他们的状态并保存数字。
  • 多个onClickListener具有LinearLayout属性以填充整个空间,每个视图(四舍五入的数字)填充空间(请参阅下面的示例以了解权重属性) - 或几个weight s - 甚至是RelativeLayout
  • TableLayout以及LinearLayout下面的GridView方法..

根据您的问题下面的评论,有很多可能性。我选择其中一个使用Linear和RelativeLayout。它可能是这样的:

ItemClickListener

<!-- named activity_main.xml --> <RelativeLayout ... > <LinearLayout android:id="@+id/contain" ... android:layout_width="250dip" android:orientation="horizontal" android:weightSum="4" > <!-- weightSum to 4 = whatever the screen, display my children views in 4 sections --> <View ... android:layout_weight="1" android:background="@drawable/green_dots" /> <!-- weight to 1 = this takes one section --> <View ... android:layout_weight="1" android:background="@drawable/green_dots" /> <!-- weight to 1 = this takes one section --> <View ... android:layout_weight="1" android:background="@drawable/green_dots" /> <View ... android:layout_weight="1" android:background="@drawable/green_dots" /> </LinearLayout> <RelativeLayout android:layout_below="@id/contain" ... > ... Here display your buttons (or textviews) with custom drawable background for each one </RelativeLayout> 课程中,它实现了Activity,如下所示:

OnClickListener

然后在你的方法里面这个Activity:

public class MyActivity extends Activity implements OnClickListener { }

然后在您的// init your buttons var Button one, two, three, four, five ...; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // set the layout above setContentView(R.layout.activity_main); // init your buttons one = (Button) findViewById(R.id.button1); two = (Button) findViewById(R.id.button2); three = (Button) findViewById(R.id.button3); ... etc. // set them to your implementation one.setOnClickListener(this); two.setOnClickListener(this); three.setOnClickListener(this); ... etc. } // call this function when one button is pressed public void onClick(View view) { // retrieves the id of clicked button switch(view.getId()) { case R.id.button1: methodToSaveNumber(int); break; case R.id.button2: methodToSaveNumber(int); break; case R.id.button3: methodToSaveNumber(int); break; ... etc. } } 方法中:

methodToSaveNumber

为了向您展示它是如何工作的,可绘制的// finally, your method to save the number of the password public void methodToSaveNumber(int i) { ... do something. ... change the state of the buttons, the dots, whatever you want } 可能是这样的:

green_dots

您必须告知您有关layouts及其工作原理的信息,click event and its listenerdrawables and their states(关注,按下,禁用,......),最后,我希望你会得到你想要的。

快乐的编码!